Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need some Help

 
pankaj semwal
Ranch Hand
Posts: 303
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi List[],


String str1="java";

String str2="java;

it return true when we do str1==str2;

please tell me why it returns true.

Thanks

 
Ravikanth kolli
Ranch Hand
Posts: 179
Chrome Eclipse IDE Mac
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is more of a beginners question.

Strings are objects in java, so they cannot be compared using the "==" operator. Using a "==" compares the reference of the string objects and thus it returns true.

comparison between Strings should be done using "equals" method.

Note: This is only when Str2 = "java";

 
Balagopal Kannampallil
Ranch Hand
Posts: 136
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to add some more points to Ravikanth's post.
In the first line you are making a new String object str1 in the String pool and in the next line you are creating another object str2 using the same literal "java". So this time it won't create a new object, it will just assign the new object to existing object str1 object in the String pool.
This happens only when you say

The scenario changes completely when you say

Try it out yourself

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is how Memory is efficiently managed by the JVM , actually these two Objects are created on to the string constant pool.
 
Emanuele Ghe
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RaviNada Kiran wrote:This is how Memory is efficiently managed by the JVM , actually these two Objects are created on to the string constant pool.


I don't think it's right. Actually, in this case:


Only 1 object exists on the string pool.

In this case;


2 objects exists on the string pool.

Am I wrong ?
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read properly in earlier case taht is
String str1 = "java";
String str2 = "java";

as i said only one object exists on to string constant pool , where in other case

String str2 = new String("java"); ,created on to the heap

 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pankaj,
Please use meaningful subject lines for your post. "Need some help" in a forum, where everybody would like some help, is hardly meaningful
 
Campbell Ritchie
Sheriff
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember: isn't the String constant pool part of the head?

There are a few classes where the JVM has been optimised to reuse values. If you have a String literal like "pankaj" then every time the class loaders find that String literal they reuse the same object on the heap. This can be done because Strings are immutable. There is similar behaviour for a restricted set of values for Integer, and probably for a few other similar immutable classes. So two String references set equal to the same String literal will actually both point to the same object on the heap, unless you say (as somebody already mentioned) "new String("pankaj")".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic