S
softwarepearls_com
Is there really no way to check whether a String is interned or not,
without interning currently non-interned Strings?
without interning currently non-interned Strings?
Is there really no way to check whether a String is interned or not,
without interning currently non-interned Strings?
softwarepearls_com said:Is there really no way to check whether a String is interned or not,
without interning currently non-interned Strings?
you can compare it with the interned version.
if ( x.intern()== x)
Well, no, I don't think so.
I'm not sure why you'd want to though. If your algorithm depends on
object identity, you should create a pool of strings yourself.
Interning is not guaranteed, afaik. Interned strings may be removed to
make room for other strings, or the call to intern() mail silently fail
and not intern the string.
Well, no, I don't think so.
I'm not sure why you'd want to though.
softwarepearls_com said:What I'm after
is something like an instance method String.isInterned(). Shame it's
not in the API.
Tom said:Really? What makes you think that? The javadoc gives no hint of such
failure modes:
Mark said:Hmm, so it's well known that Java does tricky stuff with intern'd
strings. I assumed that it could just plain run out of space and not
intern a string at all. At least, if it was critical that intern always
work, I wouldn't trust it, personally.
This article goes in to some of the tricks intern() plays. It does say
that intern() guarantees that s1.intern()==s2.intern() if s1.equals(s2),
but still I think I'd trust an implementation I had full control over
than rather than hope one I wasn't sure about provided the semantics I
needed.
Presented for the general edification:
http://www.javaworld.com/javaworld/javaqa/2003-12/01-qa-1212-intern.html
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.