• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why does "==" does not return false upon comparison of two String Literals??  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys....

I've a code snippet which goes like this::::



Now,I'm a bit confused, because since JAVA automatically creates
new
instance for every String Literal, therefore (upto my understanding) in the above mentioned code "first" and "second" would have been given references to separate objects, so why does the line under the
if
statement is displayed as the output??
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through this link and let us know if you still need any help.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyus singh wrote: . . . Now,I'm a bit confused, because since JAVA automatically creates new instance for every String Literal, . . .
Who on earth told you that? It is all explained in the good link you have already been given.
Please avoid overusing quote tags; you are using them for things which are not quotes.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhimanyus singh wrote:Hi Guys....

I've a code snippet which goes like this::::




Now,I'm a bit confused, because since JAVA automatically creates
new
instance for every String Literal, therefore (upto my understanding) in the above mentioned code "first" and "second" would have been given references to separate objects, so why does the line under the
if
statement is displayed as the output??






The String are immutable in java.So, the moment you create a string object it will be fixed in the memory. No operations can change the contents of that String object.
So, our smart JVM, takes this fact as an advantage.
When you declare anyString object , JVM first searches it in the memory to check if there is any instance lying around in the memory having the same contents. If found, it takes address the same instance and return it for the newly created string. and now we get illusion that we have created a different String object but internally it is pointing to already present instance.
And as == operator checks for instance equality, it is returning true in above case.

But if you tried, String s1="abcd";
String s2=new String("abcd");
Here, we are manually creating the second instance in s2, so here s1==s2 wil be false.


 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shailesh pratapwar wrote:
When you declare anyString object , JVM first searches it in the memory to check if there is any instance lying around in the memory having the same contents.


No, this is not how it works.

We declare variables, not objects.

And the JVM doesn't search every time we declare a String variable. Rather, whenever you put a String literal in your code, the compiler adds that String to the .class file (if it's not already there). Then, when your class is loaded, its String literals are put into the JVM's constant pool (if they're not already there) and everywhere in the code that refers to that String literal simply evaluates as a reference to the appropriate object in the pool.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!