I ran the above code using JDK6 and JDK7 respectively, and the results are as follows:
JDK6: false, false
JDK7: false, false
It is not true or false as the questioner said. I think there are two main reasons:
The string "java" is special, it exists fixedly in the constant pool
Hence the code
String str1 = new StringBuilder("ja").append("va").toString();
System.out.println(str1.intern() == str1);
str1.intern() returns an object in the constant pool, so it is not the same object as str1 on the heap.
The "python" string already appears in the code, so it will be added to the constant pool.
In the second part of the code, because the literal constant "python" appears, it will be added to the constant pool. And:
String str2 = new StringBuilder("python").append("").toString();
System.out.println(str2.intern() == str2);
The empty string "" is added to the string "python", so it is equivalent to not adding a new string constant, and str2.intern() still returns the object in the constant pool.
In fact, the above question can be extended, for example:
I just changed the first part of the code "StringBuilder("ja").append("va")" to "StringBuilder("ja").append("va1")". Will this change have any different results? ? Let’s take a look at the results of running under JDK6 and JDK7:
JDK6: false, false
JDK7: true, false
Why are the running results of JDK6 and JDK7 different? In fact, this involves different implementations of the intern() method in different JDKs: In JDK6 and previous JDKs:
The reason why different results are returned in different JDKs: In JDK6, "java1" is the first string constant that appears, so it will be copied to the constant pool. The intern() method returns the string constant in the constant pool. object, so it is not the same as str1 on the heap. In JDK7, "java1" is the first string constant that appears, but the intern() method only adds a reference to this object to the constant pool. It does not copy a new object to the constant pool like JDK6, so the reference returned by the intern() method is actually equal to the original str1.
I ran the above code using JDK6 and JDK7 respectively, and the results are as follows:
It is not true or false as the questioner said. I think there are two main reasons:
The string "java" is special, it exists fixedly in the constant pool
Hence the code
str1.intern() returns an object in the constant pool, so it is not the same object as str1 on the heap.
The "python" string already appears in the code, so it will be added to the constant pool.
In the second part of the code, because the literal constant "python" appears, it will be added to the constant pool.
And:
The empty string "" is added to the string "python", so it is equivalent to not adding a new string constant, and str2.intern() still returns the object in the constant pool.
In fact, the above question can be extended, for example:
I just changed the first part of the code "StringBuilder("ja").append("va")" to "StringBuilder("ja").append("va1")". Will this change have any different results? ?
Let’s take a look at the results of running under JDK6 and JDK7:
Why are the running results of JDK6 and JDK7 different?
In fact, this involves different implementations of the intern() method in different JDKs:
In JDK6 and previous JDKs:
For JDK7 and above:
Based on the difference between the intern() methods of JDK6 and JDK7, we know the example:
The reason why different results are returned in different JDKs:
In JDK6, "java1" is the first string constant that appears, so it will be copied to the constant pool. The intern() method returns the string constant in the constant pool. object, so it is not the same as str1 on the heap.
In JDK7, "java1" is the first string constant that appears, but the intern() method only adds a reference to this object to the constant pool. It does not copy a new object to the constant pool like JDK6, so the reference returned by the intern() method is actually equal to the original str1.