If a string were created without using the new keyword as follows:
String s = "abcd";
This string "abcd" will be placed in the special Java string pool. My question is whether this pool is subject to garbage collection or not. If it is, is there any difference in the way garbage is collected from standard heap and this special pool? I think this pool is not a target for collection, can someone pls confirm this? Thanks.
First, let me clear up a common misconception: the "String pool" is not a special area of memory where Strings are allocated; it's basically a hash table where references to Strings are kept. The Strings themselves, like all other Strings, are in the heap.
Now, will such Strings ever be garbage collected? The JLS is silent on this question. I've been told that in some implementations, they can be, but only if the class that defined them itself collected. This will only happen if the ClassLoader that loaded the class is collected; and this happens only with user-created class loaders, so it's not something you'll come across in a beginner situation.