Post by Petterson MikaelHi,
I was reading the sun java api 1.4.2 and found the following for
String.intern().
public String intern()
Returns a canonical representation for the string object.
What is a canonical representation? Any example?
Canonical means reduced to its simplest form.
In this context it means it returns a reference to the very same String
object itself, which will be a reference to one of the internally pooled
String objects.
The process of interning means that the object is added to the internally
maintained collection of String objects. Since String objects are immutable
and any two objects where str1.equal(Object str2) == true are equivalent and
are guaranteed to remain so. Because of this it is reasonable and more
efficient to allow any created String to be GC's and rely on the returned
value of intern().
Eg if you get a String object from the Standard input stream, this will be a
unique String object.
If you want to use it you may do
myInputString = myInputString.intern();
This will guarantee that you do not create extra copies of an identical
String.
Note that typically interning is automatic within a class as long as you
create your strings at compile time and like so: String s = "xyz";
Do not do String s = new String("xyz"); since that will not auto intern the
result.
I believe that runtime String instantiation is not auto interned.
But you would have to read the JLS for the exact mechanism.
--
Mike W