String objects you create with a new are eligible for gc when there is no longer a reference to them, just like any other object.
For example:
String myString = new String("I am a string");
myString = null;
This code creates a new String object, then immediately sets its reference variable to null, thus making this object eligible for gc.
Now, there is a concept of a "String literal", which is the actual text between the quotes ("I am a string"). This string literal is *also* a String object in memory, BUT this object will never be gc as long as the class in which it is defined is loaded...for most cases, and for the
SCJP, you can state that "string literals are never eligible for gc."
Here's a *similar* example as above, but with different results...
String myString = "I am a string";
myString = null;
The myString reference variable is set to null, but the
java VM keeps its own private reference to the String object on the right. That's why it's never eligible for gc.
Rob
[ January 31, 2002: Message edited by: Rob Ross ]