The answer is 0. String literals are not garbage collected. However if the code was written like this.... String string1 = new String("Test"); String string2 = new String("Today"); string1 = null; string1 = string2; The answer would be 1.

Percy Densmore you answer is correct. "test" has not be reffernce after the first execution, therefore it is eligible for garbage collection. a. 1 "is the answer"

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

posted Sep 19, 2001 20:41:00

0

I have to agree with Jimmy. The J2SE API spec for the String.intern() method states "A pool of strings, initially empty, is maintained privately by the class String." Additionally, "All literal strings and string-valued constant expressions are interned," i.e. they are added to the String pool. This implies that the String class manages "Test" and "Today" within its private String literal pool. Hence, even if "Test" is no longer referenced, the pool manager may still be holding on to it, thereby preventing the garbage collector from freeing the object from memory.