First of all, we should be clear that there is still a difference in the intern method of the String class in JDK1.6 and JDK1.7:
Intern in JDK1.6:
When calling the intern method, first, go to the constant pool to see if there is a value the same as the current String value exists. If it exists, it will directly return a reference to the String value in the constant pool; if it does not exist, the string from the original heap will be copied into the constant pool.
Intern in JDK1.7:
When calling the intern method, first, go to the constant pool to see if there is a value the same as the current String value exists. If it exists, it will directly return a reference to the String value in the constant pool; if it does not exist, it will only place the reference to the original heap in the constant pool, and will not copy the entire string into the constant pool.
This means that JDK1.6 and JDK1.7 handle differently when this string does not exist in the constant pool.
The following is an example to verify and explain:
Example:
public static void main(String[] args) { String str = "str"+new String("01");① str.intern();② String str1 = "str01";③ System.out.println(str == str1); String str2 = new String("str01");④ str2.intern();⑤ String str3 = "str01";⑥ System.out.println(str2 == str3); String str4 = "str01";⑦ String str5 = new String("str")+new String("01");⑧ str5.intern();⑨ System.out.println(str4 == str5); In JDK1.6, the output result is:
false
false
false
explain:
① During execution, a string object str with the value "str01" will be created in the heap memory, and a "str" and "01" constants will be created in the constant pool;
② When executing, first go to the constant pool to check whether there is a constant value of "str01" and find that it does not exist. JDK1.6's method is to generate a copy of the string "str01" in the constant pool;
③ During execution, a "str01" object will be created in the constant pool, and it will be found that it already exists, so it will not be created new;
The reason for the first output false is that str points to "str01" in heap memory, while str1 points to "str01" in the constant pool;
④ When executing, a string object str2 with a value of "str01" will be created in the heap memory, and a constant with a value of "str01" will be created in the constant pool;
⑤ When executing, first go to the constant pool to check whether there is a constant value of "str01". When it is found that it exists, it will directly return this constant reference;
⑥ During execution, a constant value of "str01" will be created in the constant pool. If it is found that it already exists, it will not be created;
The reason for the second output false is that str2 points to "str01" in heap memory, while str3 points to "str01" in the constant pool;
⑦ During execution, a constant value of "str01" will be created in the constant pool;
⑧When executing, a string object str5 with the value "str01" will be created in the heap memory, and a "str" and "01" constant will be created in the constant pool;
⑨ When executing, the constant pool will be used to check whether there is a constant value of "str01". If it is found, it will directly return this constant reference;
The reason for the third output false is that str5 points to "str01" in heap memory, while str4 points to "str01" in the constant pool;
In JDK1.7, the output result is:
true
false
false
explain:
I found that only the first output result is different, so we only explain the reason for the first one:
① During execution, a string object str with the value "str01" will be created in the heap memory, and a "str" and "01" constant will be created in the constant pool; (This is no different from JDK1.6)
② When executing, first go to the constant pool to check whether there is a constant value of "str01" and find that it does not exist. JDK1.7's method is to copy the reference of "str01" in heap memory to the constant pool;
③ During execution, a "str01" object will be created in the constant pool, and it will be found that it already exists, so it will not be created new;
Then at this time str and str1 will both point to the value of "str01" in the heap memory, so the two are equal;
The above is a comparison of the intern method of the String class in JDK1.6 and JDK1.7. There are differences. Friends who need it can refer to it.