According to String#intern(), intern
method is supposed to return the String from the String pool if the String is found in String pool, otherwise a new string object will be added in String pool and the reference of this String is returned.
So i tried this:
String s1 = "Rakesh";
String s2 = "Rakesh";
String s3 = "Rakesh".intern();
if ( s1 == s2 ){
System.out.println("s1 and s2 are same"); // 1.
}
if ( s1 == s3 ){
System.out.println("s1 and s3 are same" ); // 2.
}
I was expecting that s1 and s3 are same
will be printed as s3 is interned, and s1 and s2 are same
will not be printed. But the result is: both lines are printed. So that means, by default String constants are interned. But if it is so, then why do we need the intern
method? In other words when should we use this method?
Interned Strings avoid duplicate Strings. Interning saves RAM at the expense of more CPU time to detect and replace duplicate Strings. There is only one copy of each String that has been interned, no matter how many references point to it. Since Strings are immutable, if two different methods incidentally use the same String, they can share a copy of the same String. The process of converting duplicated Strings to shared ones is called interning.String.intern() gives you the address of the canonical master String. You can compare interned Strings with simple == (which compares pointers) instead of equals which compares the characters of the String one by one. Because Strings are immutable, the intern process is free to further save space, for example, by not creating a separate String literal for "pot" when it exists as a substring of some other literal such as "hippopotamus".
To see more http://mindprod.com/jgloss/interned.html
String literals and constants are interned by default. That is,
"foo" == "foo"
(declared by the String literals), butnew String("foo") != new String("foo")
.On a recent project, some huge data structures were set up with data that was read in from a database (and hence not String constants/literals) but with a huge amount of duplication. It was a banking application, and things like the names of a modest set (maybe 100 or 200) corporations appeared all over the place. The data structures were already large, and if all those corp names had been unique objects they would have overflowed memory. Instead, all the data structures had references to the same 100 or 200 String objects, thus saving lots of space.
Another small advantage of interned Strings is that
==
can be used (successfully!) to compare Strings if all involved strings are guaranteed to be interned. Apart from the leaner syntax, this is also a performance enhancement. But as others have pointed out, doing this harbors a great risk of introducing programming errors, so this should be done only as a desparate measure of last resort.The downside is that interning a String takes more time than simply throwing it on the heap, and that the space for interned Strings may be limited, depending on the Java implementation. It's best done when you're dealing with a known reasonable number of Strings with many duplications.
I want to add my 2 cents on using
==
with interned strings.The first thing
String.equals
does isthis==object
.So although there is some miniscule performance gain ( you are not calling a method), from the maintainer point of view using
==
is a nightmare, because some interned strings have a tendency to become non-interned.So I suggest not to rely on special case of
==
for interned strings, but always useequals
as Gosling intended.EDIT: interned becoming non-interned:
In version 2.0 maintainer decided to make
hasReferenceVal
public, without going into much detail that it expects an array of interned strings.Now you have a bug, that may be very hard to find, because in majority of cases array contains literal values, and sometimes a non-literal string is used. If
equals
were used instead of==
thenhasReferenceVal
would have still continue to work. Once again, performance gain is miniscule, but maintenance cost is high.Learn Java String Intern - once for all
Strings in java are immutable objects by design. Therefore, two string objects even with same value will be different objects by default. However, if we wish to save memory, we could indicate to use same memory by a concept called string intern.
The below rules would help you understand the concept in clear terms:
Example:
Note: The motivational cases for string intern are not discussed here. However, saving of memory will definitely be one of the primary objectives.