This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Remember that a variable is not an object; it points to an object. You can have more than one variable all pointing to the same object.
The expression 's + "hi"' creates a new String, and the variable "s" is set to point to it. The original String object that "s" pointed to is unchanged.
"java" doesn't get destroyed; it's just not referenced anymore. For most objects, that means the garbage collector will eventually delete it. For this particular String, since it's a literal (a String constant that appears in the source code) it's held in the string pool, so it won't be GCd.
Hi friends, can anybody tell me why st1 == st4 is false and st1 == str5 is false.
Thank you all in advance. Have a nice day.
There is no rule that we all should know everything. Lets learn few things of everything here.
Ankur Jain Kothari
Ranch Hand
Joined: Feb 08, 2010
Posts: 154
posted
0
String s4 is a new object...so calling == gives false as they are different objects...since s5 is "change me" + words....this creates a new string in the pool...so s1 and s5 refers to different objects in the string pool...s5 is not a constant because of this
Do What You Wanna Be....Taking Things The Way They Come
scjp 1.6 91 percent, scmad 90 percent(rounded off to nearest integer)
krishna Karthikk
Ranch Hand
Joined: Mar 16, 2010
Posts: 92
posted
0
Hi Ted can you be more specific, really I haven't understood.
As you said str1 and str4 are different objects, I got a false. Then str1 and str2 are also different objects, but they are returning true. Why?
krishna anusha wrote: Then str1 and str2 are also different objects, but they are returning true. Why?
Because they are not different objects. The strings assigned to str1 and str2 are compile time constants, so the compiler uses the string pool, and hence, they refer to the same string object.