String intern understand methods "in-depth understanding of the Java Virtual Machine," 2nd edition finally dug pits in the third edition are filled in large R

"In-depth understanding of the Java Virtual Machine," 2nd edition finally dug pits in the third edition are filled in large R

intern role

The role of this method is to load a string for the first time encountered the constant pool.

For any two strings s and t, if and only if s.equals (t) is true when, s.intern () == t.intern () it is true.

Test code

     String str1 = new StringBuilder("计算机").append("软件").toString();
        System.out.println(str1.intern() == str1);

        String str2 = new StringBuilder("ja").append("va").toString();
        System.out.println(str2.intern() == str2);

In running this code JDK 6, two will be false, and runs in JDK 7, will be a true and a false.

Because of the constant pool is located in JDK 6 method District, JDK 7 after constant pool is located in the heap, so to run the above two versions of the code will appear jdk amazing things. Even 8 to run with JDK, you can not think there will be results.

The variance is: In JDK 6, intern () Copy String example method will first encounter a string constant pool to store permanent generation, it is the return of the string inside the permanent generation of a reference example, and string StringBuilder instance created by the Java heap, it is not necessarily the same references, returns false.

The JDK 1.7 (as well as some of the other virtual machines, such as JRocki) the intern () do not need to implement a copy of instances of the string to the permanent generation, since the string constant pool has been moved to the Java heap, you would only need the constant pool examples of what was recorded for the first time a reference to, and therefore intern () references and the string instance created by the same StringBuilder is returned. Output false, explained before calling the main method must have been the "java" string in a string constant pool inside, and not for the first time, and was there before the new Stringbuilder, inconsistent with the principle intern first appears. The new StringBuilder in to the newly created object, of course, vary.

debug

We marked the first line of the main method of a breakpoint, the debug run the program, you can see the Memory, and then filtered out String, as follows:

 

 Then double filtered out java.lang.String, you can see the following:

 

 On this page we can continue to filter:

 

 Sure enough, before the program is not executed, line 14, "java" has emerged.

From this result we can infer: the Java standard library is loaded in the JVM startup section, which may have a class has a reference to "java" string literal, when the literal is referenced for the first time will be intern, joined string constant pool to go.

sun.misc.Version's init () method of loading.

 

Different versions of java java string is not necessarily loaded

For example, the example I ran out on JDK8u212-b03, are two true:

In this version there, sun.misc.Version of launcher_name become "openjdk":

 

 那么根据我们之前的猜测,把程序成下面这样的,效果就是一样的了:

 

Guess you like

Origin www.cnblogs.com/fanguangdexiaoyuer/p/12336369.html