Question

I understand that String declared as the following will be saved in the constant pool:

eg.

String a = "test'; 

then test will be saved in the pool

I wonder if the String acquired from a method, eg.from a request parameter posted from a web form, or read from a file. Eg.

String name = request.getRequestParameter("name");

Will the value in name be saved in constant pool? I assume it's not? because otherwise you would soon have out of memory perm gen error?

Can anyone please enlighten me ?

Thanks

Was it helpful?

Solution

No. When and how strings are interned is described in the String#intern documentation and the JLS Section 3.10.5. Basically, literals and (other) string constants are auto-interned, but nothing else is likely to be unless you do it explicitly.

As a side point, you seem to be assuming that interned strings are never garbage-collected. I don't see anything in the Java documentation actually saying that, and at least one person claims that his tests show that they are, in fact, GC'd, suggesting that the intern pool uses some form of weak reference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top