I'm learning Java now, and i read this question:What is Java String interning? - Stack Overflow
but i read other articles which provide some examples i don't understand:
public static void main(String[] args) {
String str2 = new String("str") + new String("01");
str2.intern();
String str1 = "str01";
System.out.println(str2 == str1); //true
String s4 = new String("1");
s4.intern();
String s5 = "1";
System.out.println(s4 == s5); //false
String s = new StringBuilder("aa").append("bb").toString();
String s2 = new StringBuilder("cc").toString();
System.out.println(s.intern() == s); //true
System.out.println(s2.intern() == s2); //false
}
the result in Java 11( it should be same in Java 8) is :
true
false
true
false
i don't know why results are different, i suppose it to be all true. can anyone explains it?
Let's look at what
internactually does (emphasis mine):When this code is run:
The string pool only contains
"str"and"01".str2is"str01", which is not in the string pool, so the object thatstr2refers too is added to the string pool. Therefore, when the next line is reached, since"str01"is already in the pool,str1will refer to the same object asstr2.Note that the reason why
str2 == str1is not becauseinternsomehow changes which objectstr2refers to. It doesn't do anything tostr2. Strings are immutable after all.Now we can understand the first
false. In the line:Because of the string literal
"1","1"is added to the string pool. Note that the object that is added to the string pool is not the same as the object to whichs4refers. Now you calls4.intern(), which does nothing tos4, and returns the object that is in the string pool. But you are ignoring the return value. This is whys4 != s5, ands4.intern() == s5.The reason for
s2.intern() != s2is much simpler -s2refers to a different object than"cc"in the string pool!s2.intern()is supposed to return the object in the string pool, so of course it is not the same object!