Forums Register Login

IF strings are immutable why is the variable s appending and changing its obect

+Pie Number of slices to send: Send
class StringTest
{
public static void main(String args[])
{
String s="java";
s=s+" hi";
System.out.println(s);
}
}
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
you mean to the s variable is pointing towards teo objects? "java" and "java hi"
+Pie Number of slices to send: Send
Yes. First it points to "java" and then it is moved to point to another completely separate String containing "java hi".
+Pie Number of slices to send: Send



is this diagram correct
+Pie Number of slices to send: Send
"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.
+Pie Number of slices to send: Send
try this program.
+Pie Number of slices to send: Send



so will java object be garbage collected
+Pie Number of slices to send: Send
yes
+Pie Number of slices to send: Send
If it is no longer necessary, yes. But as Ernest said, that may happen later than regular objects, if the String pool decides to hang onto it.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.

Henry
+Pie Number of slices to send: Send
Hi Henry, I understood now, thanks for the quick reply.
+Pie Number of slices to send: Send
yup s1 and s2 are the objects...
I brought this back from the farm where they grow the tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1675 times.
Similar Threads
strings
converting an input string to a double
Generic question
object
GC
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 21:00:06.