For your sake, can you please tell me where you think this "Pool String" lives? ...and how did you conclude that objects that are created for String literals are never garbage collected?
Strings literals can be garbage collected if and only the class loader which loaded their String class is garbage collected. This will only happen if your code do not have any other String object declared whose class is loaded by the bootstrap loader.
Prove that X.s and Y.s can refer to two different instances during runtime. It can be done. Note that both are compile-time constant expressions (JLS 15.28) and so are inlined at compile-time.
They can refer to two different instances if they are loaded by a different class loader as long as a bootstrap class loader or any other loader have not loaded a string literal like the ones contained in X and Y.
So it is not true what I said that String literals are never garbage collected, altough it was true in the context of the initial post.
I did not changed my mind regarding the existance of the String pool and still do not know why you clamied the JLS was erroneous in that point. Yet I see the string pool as an array of references to String objects in memory. I think I never said they were not in memory. [ February 02, 2005: Message edited by: Edwin Dalorzo ]
posted 11 years ago
A String literal is not garbage collected, since it is a String literal, not an object. It is the object that represents the String literal that is garbage collected.
The answer to your original question is b) as per my original post.