Originally posted by alex earnshaw:
To my knowledge, "Test" will be eligible for garbage collection immediately after line 3. "Today" will be eligable for garbage collection only if both string1 and string2 are set to null or to other string objects.
HTH
Alex
I believe that since the code is using string literals, "Test" is never eligible for collection.
The string literal is placed into the pool and as such has something always referencing it.
Now if the code said:
String string1 = new String("Test");
String string2 = new String("Today");
string1 = null;
string1 = string2;
then, the first object is eligible when string1 is set to null.
Of course if I'm wrong someone will correct me