About the intern method of String class in java

About the intern method of String class in java

Antecedents

During the review, I thought I understood the meaning of the String intern method when I wrote the section of common classes in notes, which is to return the address of the String constant in the String constant pool!

String s1 = new String("zs");
String s2 = s1.intern();

System.out.println(s1 == s2);

s1 points to the instance in the heap and s2 points to the string in the String Pool, so it must be false!

I found a blog while searching for information In depth parsing of string constant pool_ River blog - CSDN blog_ String constant pool , looking at the blogger, I have some questions

String s3 = new String("1") + new String("1");
String s5 = s3.intern();
String s4 = "11";
System.out.println(s5 == s3);
System.out.println(s5 == s4);
System.out.println(s3 == s4);

System.out.println("======================");

String s6 = new String("go") +new String("od");
String s7 = s6.intern();
String s8 = "good";
System.out.println(s6 == s7);
System.out.println(s7 == s8);
System.out.println(s6 == s8);

s3 is not the StringBuilder object calling the append method to complete the splicing and finally calling the toString method. It is the object of the new new, while s5 and s4 point to the "11" in String Pool.

false,true,false
======================
false,true,false

What's so hard about that?

But the result surprised me:

It's all true, which makes me wonder&_&

attempt

I tried to put S3 The line intern() is commented out, and the result is false???

Wow, this intern method is so magical that I began to think about it... Is it difficult that the bottom layer of the intern method secretly changed the address saved in s3 into the memory address of "11" in the constant pool.

So I immediately revised the notes about intern from beginning to end and revised the relevant definitions, but I still don't think it's right. Can intern do so? So check the notes here again

Otherwise, add the String object to the pool and return a reference to the String object. This sentence finally attracted my attention. Is it difficult that the String object created by this splicing method will not create "11" in the pool (it will be created in the pool only when a literal quantity is used for the first time), while this new String("1")+new String("1") uses the literal quantity of "1", and there is only "1" in the pool. Therefore, when calling the intern method, because there is no "11" in the pool, the s3 instance reference is added to the pool and the s3 instance reference is returned.

Then String s4 = "11"; It uses the equals method in the String Pool to find an instance (Reference) with the value of "11", that is, the s3 instance, and returns the s3 instance reference to s4.

Premise: HotSpot VM, String object calls the intern method, and uses the equals method to determine whether the two objects are equal

  • In JDK7 and before, if the pool contains a String equal to the String object, the address of the String is returned; If this String is not included, a String with the same value as the String object is created in the pool and its address is returned.
  • In JDK8 and later, if the pool contains a String equal to the String object, the address of the String is returned; If this String is not included, a reference is created in the pool, which points to the String object and returns its reference.

My is JDK 8. Since each string in the pool is unique, "11" refers to the s3 instance that has been added to the pool, rather than re creating a string "11" in the pool.

Then the above three truths can be understood.

String s6 = new String("go") +new String("od");//The StringBuilder instance is created in the heap to complete the splicing, the instance with the value of "go","od" and "good", and the "go" and "od" instances are created in the constant pool
String s7 = s6.intern();//Since there is no "good" in the constant pool, add the reference of s6 instance to the constant pool and return this reference to s7
String s8 = "good";//An instance (Reference) with a value of "good" already exists in the constant pool, that is, a reference to the s6 instance. It will not create "good" because the strings of the constant pool are unique. Return the reference of s6 instance to s8.
System.out.println(s6 == s7);//s6 and s7 point to the same instance: true
System.out.println(s7 == s8);//true
System.out.println(s6 == s8);//true

reference material

(1 message) when did the Java string literal enter the string constant pool_ Tom Andersen's blog - CSDN blog

1.1.15 common classes - Xiao Chen Xue java programming - blog Garden (cnblogs.com)

Keywords: Java Back-end

Added by kishanprasad on Sun, 20 Feb 2022 01:47:00 +0200