Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Garbage collection & string literal pool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Garbage collection & string literal pool" Watch "Garbage collection & string literal pool" New topic

Garbage collection & string literal pool

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Ok. I am over it too.

These are my conclusions:

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 ]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: Garbage collection & string literal pool
It's not a secret anymore!