I have the code like this:
String str1 = new StringBuilder("计算机").append("软件").toString();
System.out.println(str1.intern() == str1); //true
String str2 = new StringBuilder("ja").append("va").toString();
System.out.println(str2.intern() == str2); //false
String str3 = new StringBuilder("Str").append("ing").toString();
System.out.println(str3.intern() == str3); //true
I can understand why str1.intern() == str1
and str3.intern() == str3
are true, but I don't understand str2.intern() == str2
.
Why this is false?
My java version is: 1.8.0_73
"java"
is obviously already in the constant pool, sostr2.intern()
just returns that reference, which is different from the newstr2
reference. – Prosecutor