I just want to clarify for everyone what's on the exam and what's NOT!
Understanding String immutability IS on the exam, and it's big!
Understanding when (non-String), objects are eligible for GC IS on the exam, and it's big and complicated!!!
However, because the String constant pool is a bit vague, the exam WILL NOT ask you to know when String objects are eligible for the GC.
So I'd recommend that you start this thread all over again using objects of a type other than String - otherwise this whole thread is confusing and misleading!
Originally posted by Vijayakeerthy Parsuvanath:
But how many String objects will be lost in your code ?
Now tell me in the following code how many String objects will be created before the println statement in the following code
I think it should be 5 because the line String s2 = new String("xyz");
creates two objects one in the normal (nonpool) memory and the other
in the pool memory (i.e. for the literal "xyz").
String objects created as follows. "winter","winter"(lost),
"winter summer","fall"(lost),"winter fall(lost),"winter summer winter(lost),
"winter"(lost),"winter winter" .
String s1 = "winter"; //1
String s2 = s1 + "summer"; //2
s1 +="winter"; //5
System.out.println(s1 + " " + s2);//6
Originally posted by Gian Franco Casula:
...(before and not including the println statement)