Java memory region

See section 1 of Thread Safety

There are two ways to use a string constant pool:

  • Literal usage
  • String.intern () method

An analysis of two ways to create string objects

literal

String s1 = "abc";
String s2 = "abc";
Copy the code

Analysis: How many objects are created? When creating an object as a literal, we first look for “ABC” in the string constant pool and return the index to s1 if it exists. If not, an object is created in the string constant pool and the index of this object is returned to S1.

The enclosed

The new keyword

String s3 = new String("xyz");
String s4 = new String("xyz");
Copy the code

Analysis: How many objects are created? If an object is created using a new method, it first looks for “xyz” in the string constant pool. If it exists, it does not create the object. If not, the object is created in the string constant pool and then “xyz” is created in the heap, returning the index in the heap.

The enclosed

String.intern () method

In JDK 6, the intern() method copies the first encountered string instance into the persistent generation’s string constant pool and returns a reference to the string instance. JDK 7 (and some other virtual machines, For example, the implementations of the JRockit () method do not need to copy the string instances to the permanent generation. Since the string constant pool has been moved to the Java heap, it only needs to record the first instance references in the constant pool

A few examples

String str1 = new StringBuilder("Computer").append("Software").toString(); 
System.out.println(str1.intern() == str1); 

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

Analysis: what are the results of JDK6 and JDK7? The difference between JDK6 and JDK7 in this respect is that the string constant pool has been moved from the permanent generation to the heap. Affect intern() implementation differently. “Computer”, “software”, “JA”, “va”, these literals will be created in the constant pool. Computer software “, “Java” will be created in the heap. Perform intern ()

  • 1.6″ Computer software “will make a copy to the constant pool, so the two addresses are not equal; Java “returns the address first put in because the constant pool already exists.
  • 1.7″ Computer software “saves the addresses in the heap directly, so the two addresses are equal; Java “returns the address first put in because the constant pool already exists.

A classic example

    String s1 = "Hello";
    String s2 = "Hello";
    String s3 = "Hel" + "lo";
    String s4 = "Hel" + new String("lo");
    String s5 = new String("Hello");
    String s6 = s5.intern();
    String s7 = "H";
    String s8 = "ello";
    String s9 = s7 + s8;
    //System.out.println(s1 == s2); // true
    //System.out.println(s1 == s3); // true
    // System.out.println(s1 == s4); // false
    // System.out.println(s1 == s9); // false
    // System.out.println(s4 == s5); // false
    // System.out.println(s1 == s6); // true
Copy the code

SQL > select * from string constant pool (s1, s1, s1, s1, s1, s1); The second line looks for “Hello” in the string constant pool, and returns s2. The compiler optimizes the third line to say “Hello”, the same as the second line; In line 4, select “Hel”, “lo” from string constant pool, create object on heap, return index to S4; If “Hello” is found in the string constant pool, create a new object on the heap. Sixth row s5 string to string constants pool to find, is found, returns to the s6 DiQiBa line go to string constants to find the “H”, ello pool, not to create objects, returns to s7, s8, have a direct return to s7, s8; (new StringBuilder()).appEnd (var7).appEnd (var8).toString(); Obviously objects are created in the heap

Attached is the decompiled bytecode code

conclusion

Some of the problems associated with creating a reference to a string should be solved. Lambda to the minus lambda to the V

References:

  1. String constant pool in-depth parsing