Checking for interned Strings?

  • Thread starter softwarepearls_com
  • Start date
S

softwarepearls_com

Is there really no way to check whether a String is interned or not,
without interning currently non-interned Strings?
 
R

Roedy Green

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)
 
M

Mark Space

softwarepearls_com said:
Is there really no way to check whether a String is interned or not,
without interning currently non-interned Strings?

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.

Plus, making your own shouldn't be hard. String overloads equals() for
you, so you could just use a set.

Set<String> intern = new HashSet<String>();

....

if( ! intern.contains("some string") ) {
intern.add("some string");
}

This basically does the same thing as you asked for yes? The problem
would be memory if the set grew very large.
 
S

softwarepearls_com

you can compare it with the interned version.

if ( x.intern()== x)

Your test will intern currently non-interned Strings. What I'm after
is something like an instance method String.isInterned(). Shame it's
not in the API.
 
T

Tom Anderson

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.

I agree. But ...
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.

Really? What makes you think that? The javadoc gives no hint of such
failure modes:

http://java.sun.com/javase/6/docs/api/java/lang/String.html#intern()

tom
 
S

softwarepearls_com

Well, no, I don't think so.

I'm not sure why you'd want to though.

A test would be useful. I can understand why String doesn't expose
such a test though. If intern() is native, then the test would almost
definitely also need to be. It's sensible to keep the native stuff to
an absolute minimum.
 
D

Daniel Pitts

softwarepearls_com said:
What I'm after
is something like an instance method String.isInterned(). Shame it's
not in the API.

How would it be useful to you? What benefit does it give to your design
or your end-users?
 
M

Mark Space

Tom said:
Really? What makes you think that? The javadoc gives no hint of such
failure modes:

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
 
D

Daniel Pitts

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

There was a time when I thought that intern()ed Strings would never be
garbage collected, but I was wrong about that. I believe that if you
run out of space upon interning a String, then you have run out of space
for the String itself.

Granted there could be space for the internal structure used for the
intern table, but my guess is that kind allocation would also result in
an OOME, so it doesn't matter from the application standpoint. intern()
succeeds with as much chance as new Object().
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top