public static void main(String[] args) {
String s1 = new String("aa");
s1.intern();
String s2 = "aa";
System.out.println(s1 == s2);
//wrong in JDK1.6 but true in JDK1.8
String str1 = new String("str") + new String("01");
str1.intern();
String str2 = "str01";
System.out.println(str1 == str2);
}
I run the code above with JDK1.8, and I think I will get two "falses" as result, because in my opinion, it is obvious that s1 & str1 are located in the heap, and s2 & str2 are interned in the string pool, but I got a "false" and a "true". The question comes: what causes the "true"?
Above is the primitive question. Now to identify this question is far from these called duplicated questions, I would like to talk about my new finding: the second part of the code gets a "false" result with JDK1.6 while a "true" result with JDK1.8. Some blogs say the behaviour of intern() has changed after the release of JDK1.7.
If the pool dosen't contain a string equals to this String object, this String object will not added to the pool and a reference to this String object will be added to the pool instead. It means the reference in the pool will be assigned to the string object located somewhere else(like the heap), and next time the initialization of a literal string equals the early one will also be assigned to the string object., which exactly describes the code part2 about the "true" result.
This theory does work on my way to explain result of the code above. But it is obvious that the theory is out of what the intern() doc contains, which is almost the same in JDK6/8 API.
Now the question comes as: Is there any better explaination for the different results of the same code in JDK1.6 & JDK 1.8? Is the theory I mentioned above exactly what truly to happen?
First let's look at the Java docs for String.intern()
:
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
Then let's look at your code:
final String s1 = new String("aa");
s1.intern();
final String s2 = "aa";
System.out.println(s1 == s2);
Here you create 3 String objects. The String literal "aa"
, which is added to the String pool, the new String("aa")
, which is a copy constructed from "aa"
, but is a different instance, and the 2nd String literal "aa"
, which is taken from the pool again (so it's the same instance as the first "aa"
). When you invoke intern()
, the String pool already has a String equal to s1
(the literal "aa"
it was constructed from), so "aa"
is returned, but you don't use it and s1
remains a different instance. Thus s1 == s2
is false
.
final String str1 = new String("str")+new String("01");
str1.intern();
final String str2 = "str01";
System.out.println(str1 == str2);
Here you create 6 String objects. The literals "str"
and "01"
, which are put into the pool. Then two copies created from those literals. Then a concatenation of those two Strings, which will be str1
. This concatenation is not placed into the pool immediately, but it will be placed into the pool when you invoke intern()
on str1
, so str1
itself is now in the pool. So when you create str2
as String literal "str01"
, it will be taken from the pool, and thus be the same instance as str1
.
String.intern() returns the a canonical representation for the string object, so you need to assign it if you want a reference to that canonical representation in the String pool.
String s1 = new String("aa");
s1.intern();
Here s1
is still the Object you declared, and is in the Heap.
You need to do
String s1Itern = s1.intern();
if you want the literal string
s1 == s2 is false
s1Itern == s2 is true
For the second case Max's Answer explains it well.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With