Hi,
In principle the answer is correct:
String string1 = "Test";
String string2 = "Today";
string1 = null;
string1 = string2;
Objects are ellegible for garbage collection as soon as there is no reference to the object anymore, so
in line 3, the reference to "Test" is set to null. in line 4 string1 refers to "Today", so there is no reference to the string object "Test" anymore, which would make it ellegible for garbage collection.
However keep in mind that (but this is probably besides the point of this particular question):
- you can not guarantee that objects are garbage collected, you can only state that an object is ellegible for garbage collection
- string literals behave differently -> in fact string literals are created in a pool of string literals, and they are not ellegible for garbage collection.
Greetings,
TK

[ May 26, 2002: Message edited by: Thomas Kijftenbelt ]