The reason is that a String
is immutable. You can't alter it without creating a new instance. So it is safe to use the same instance where ever possible for matching strings. Strings are constant; their values cannot be changed after they are created.
How ever arrays are a different story. You can alter a array, just my assigning a new value to one of the fields.
s1[0] = "testString3"
If the compiler would use the same instance for s1
and s2
you would atomatically change both arrays. And that is likely not something you want to do. That is the reason why arrays are not interned.
Please also note that the string interning may have limits depending on the compiler and the compiler may choose to not intern some strings, depending on how many strings there are or how long the strings are. There is a internal string table that is controlled by the JVM option +XX:StringTableSize=n
that determines the size of the string table used to store interned strings.
When it comes down to comparing strings is is always better to use the equals functions. Most implementations check for reference equality first anyway, before performing a more expensive checks.
EDIT:
So actually my claim that the storage for interned strings grows full seems to be wrong. The documentation of the String.intern()
method indicates that this function will make sure that the string is added to the pool of unique strings. Means that there is no way that this pool will be full. @Holger wrote that the internal implementation uses a hash map like structure of some some sort. This supports that claim.
So the JVM will store all constant strings in the interned hash table according to JLS §3.10.5.
a string literal always refers to the same instance of class String. This is because string literals - or, more generally, strings that are the values of constant expressions …- are "interned" so as to share unique instances, using the method String.intern
All that being said, please still get used to the equals
method of the string class to check strings for equality. The method will utilize the fact that the strings may be the same reference and complete very fast in that case, before performing more expensive length and by character checks. It is always better to use this method. The way how strings are handled may or may not change in future versions of java. With the equals
method you are on the safe side.