In the general case, it's simply no longer referenced by the variable, and may be eligible for garbage collection if nothing else references it. In the specific case you've shown, though, it's a String literal, which the JVM keeps in a special "String pool", so that it won't be collected. If the String was created in some other way, though, it might be.
This is an interesting area. Strings are objects like any others. But strings that are also "constants" get special treatment. See Strings, Literally here on the ranch for details.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Aug 12, 2004
O I said it's on the heap somewhere floating around and ready for System.gc() to grab it up. What is this thing about "String pool"? How would I have to write it in order for it to be eligible for garbage collection?
author and iconoclast
The String pool is a special collection of Strings which are defined in class files; the JVM uses it so such things can be shared and not duplicated.
A String literal -- a double-quoted String -- will always be stored in the String pool, and won't be collected (at least, in theory, until the class that defines it is collected.) There's no way to remove it from the pool.
Any other String -- one that comes from new String(), from BufferedReader.readLine(), etc, etc, -- will behave as you've described.
EXCEPT if you call intern() on it -- which will copy it into the String pool, and return that pooled copy.