| Author |
Need some Help
|
pankaj semwal
Ranch Hand
Joined: Oct 07, 2008
Posts: 300
|
|
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
Joined: Feb 10, 2008
Posts: 179
|
|
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";
|
-kolli
|
 |
Balagopal Kannampallil
Ranch Hand
Joined: Oct 18, 2004
Posts: 111
|
|
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
|
Bala
SCJP 5.0
Gonna hunt down SCWCD soon..
|
 |
RaviNada Kiran
Ranch Hand
Joined: Jan 30, 2009
Posts: 528
|
|
|
This is how Memory is efficiently managed by the JVM , actually these two Objects are created on to the string constant pool.
|
If you want something you never had do something which you had never done
|
 |
Emanuele Ghe
Ranch Hand
Joined: Feb 04, 2009
Posts: 111
|
|
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 ?
|
SCJP6 with score 90%. I am conscious of my ignorance and ready to learn from everyone.
|
 |
RaviNada Kiran
Ranch Hand
Joined: Jan 30, 2009
Posts: 528
|
|
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
Joined: Jul 26, 2007
Posts: 8432
|
|
Pankaj,
Please use meaningful subject lines for your post. "Need some help" in a forum, where everybody would like some help, is hardly meaningful
|
[Donate a pint, save a life!] [How to ask questions] [Onff-turn it on!]
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
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")".
|
 |
 |
|
|
subject: Need some Help
|
|
|