• 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

== with Strings is giving a different result

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following

Why isn't s1 identical to s4 but s2 is identical to s1!!!
 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Coz == operator compares object references(memory addresses which is bit representaion),but equlas() method compares objects .Coz equals() method is overriden in String class.
s1,s2,s3,s4 all are different objects reffering to different memory locations on the heap .So if you compare any of the above 2 objects using == ,it returns false.
Veena
 
Ranch Hand
Posts: 1400
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The behaviour of equals() function is fine.
Regarding == comparison, the string s1 and s2 are pointing to same object in the compiler's pool whereas s4(=s3 + "c")is an object on heap and hence is not pointing to the "abc" object present in the pool.Hence, s1 and s2 are same whereas s4 is pointing to different location.
You can also try to add a String s5 = s1; and comparing s1 == s5 is returning true.As both are pointing to object in compiler's pool.
Further, to verify this try to add this line of code ---->
if (s1 == s4.intern())
System.out.println("s1==s4");
<----
This returns true, as calling intern method, returns the string form of s4 object from the pool.
Hope this helps.
Varun
[ June 25, 2003: Message edited by: varun Khanna ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mo
It's so because s1 and s2 refer to the same thing because "abc" and "ab" + "c" are both compile time constants. So the value is calculated at compile time. But the value of s3 + "c" is calculated at runtime and hence s3 + "c" is not in the pool. That is s3 + "c" is a freshly made string and hasn't been interned so a new memory location is assigned to it. Though both the Strings are identical except the fact that their memory locations are different.
[ June 25, 2003: Message edited by: Anupam Sinha ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what the compiler does for �ab� + �c�;
s2 = �abc�; //store a reference to String object in this variable.
The concatenation of two compile-time String constants is a compile-time String constant.
Here is what the compiler does for s3 + �c�:
StringBuffer sb = new StringBuffer();
sb.append(s3);
sb.append(�c�);
s4 = sb.toString(); //create a new String object
The concatenation of a String variable and a compile-time String constant is a run-time computation to create a new String object.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even if you are not familiar with byte-codes, it can be somewhat convincing to see what the compiler is doing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic