• 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

Can any one solve this == &(==)

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String s="abc";
String s1="abc;
System.out.println(s==s1); false
System.out.println((s==s1); true

Why is it so?

I some where read like jvm maintains constant pooling as there are many objects created for string every time.

Can any one help in this?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Answer for both that is true only
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That code would not even compile.

As the guys running this have asked a billion times, please quote source examples and copy them correctly!
 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i do
String s=new String("abc");
then in both cases it returns false,i am not clear
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


produces :

true
false
true
true

Reason: Strings are handled differently that other variables. They are immutable and once created they are stored in a string pool for reuse (note, the strings themselves NOT the references). If a reference is created to a string that already exists ("abc") then that string is reused from the string pool, as it can never change - i.e there is only one string "abc" created above.
However, there are 3 string references created s, s1 and s2. The first thing to understand is the the == checks if the references refer to the same object in memory, it does not check if their contents are equal. Here, s and s1 refer to the same object on the heap and hence s == s1 returns true. However, s2 is created using the new operator meaning that s2 refers to a new object on the heap meaning that s == s2 will be false (they do NOT refer to the same object).

To check if they are meaningfully equal (using their values) you use the .equals() method. This has been overridden by the String class to check if the strings that the string reference variables refer to are equal. Hence both s.equals(s1) and s.equals(s2) return true.

You should always use the .equals() method to check if objects are meaningfully equal. This method (along with hashcode()) should be overridden in a class that you write if you need to know what makes the object equivalent. For example:



Hope this helps
/Tom
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Forgot, also :
 
The glass is neither half full or half empty. It is too big. But this tiny ad is just right:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic