• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

String comparision

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class test {
public static void main(String args[]) {
String s1 = "abc";
String s2 = "abc";
if(s1 == s2)
System.out.println(1);
else
System.out.println(2);
if(s1.equals(s2))
System.out.println(3);
else
System.out.println(4);
}
}

what is the output?
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Print out:
1
3
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wouldn't it be better to compile and run this yourself instead of posting the question here?
Now, if you do this and then don't understand why the program behaved like it did, you can ask a better question.
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes I did compile and run before asking question
but
Integer i = new Integer(5);
Integer i1 = new Integer(5);
i == i1, returns false
and
String s = "abc";
String s1 = "abc";
s == s1 returns true
that was bit confusing
but anyway Thanks for the idea of asking me to compile and run and putting a better question.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, that's a much better question.
Each time you create a "new Integer", you get a different Integer object. When you do "i==i1", you are comparing the object references, and these objects are not the same, so it's false.
The compiler treats String literals specially. Every instance of the string literal "foo", no matter where it appears, is the same String object. So "s==s1" is true.
This is true only of String literals, not Strings in general. If you had
String s = "abc";
String s1 = s.charAt(0) + "bc";
then s==s1 will be false. The two strings have the same contents but are different objects.
[ October 31, 2002: Message edited by: Ron Newman ]
 
Jay Ashar
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for explanation, I didnt know dumb questions were not allowed here.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The question is not "dumb". However, if the question is "what does this code do", the best way to find out the answer is to compile and run it. Then come back here and ask why it behaved the way it did.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic