So the question here is pretty simple: is there a way to tell if a String in Java is interned? My guess is no, but I’m wondering if anyone knows better.
So the question here is pretty simple: is there a way to tell if
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The only way you can find out if a
Stringis interned is to callintern()and check if it returns itself:This obviously has the drawback of interning the
Stringwhen it wasn’t interned before.Going partially off-topic, there’s a way to do “custom” interning with an explicit pool using the
Internerinterface of Guava (using the implementations exposed by theInternersclass). This has the advantage of being able to let theInterneritself (and thuse the pool) being garbage collected when it’s no longer referenced.