String characteristics and string constant pool (use of intern() and detailed explanation of string splicing)

12. Characteristics of string

12.1 basic characteristics of string

  • String is declared as final and cannot be inherited

    • String s1 = "ILOVEYOU";// Definition of literal quantity
    • String s2 = new String("ILOVEYOU");
  • String implements the serializable interface: it means that the string supports serialization (data transmission across processes). The Comparable interface is implemented: it means that strings can compare sizes

  • String defines final char[] value in jdk8 and before to store string data. Change to byte []

  • String: represents an immutable character sequence. Immutability

    • When the string is re assigned, the assigned memory area needs to be rewritten, and the original value cannot be used for assignment.
    • When connecting an existing string, you also need to assign a value to the memory area again. The original value cannot be used for assignment.
    • When calling the replace() method of string to modify the specified character or string, it is also necessary to re specify the memory area assignment, and the original value cannot be used for assignment.
  • Assign a value to a string by literal means (different from new). At this time, the string value is declared in the string constant pool.

Strings with the same content will not be stored in the string constant pool

12.1.1 StringTable

  • The string Pool of string is a Hashtable of fixed size. The default size length is 1009. If there are too many strings in the string Pool, the Hash conflict will be serious, resulting in a long linked list. The direct impact of a long linked list is when calling string When intern, the performance will be greatly reduced.
  • Use - XX:stringTablesize =? The length of the stringTable can be set
  • In jdk6, the stringTable is fixed, which is 1009 in length. Therefore, if there are too many strings in the hanging pool, the efficiency will decline rapidly. StringTablesize setting is not required.
  • In jdk7, the default value of the length of stringTable is 60013.
  • Starting from jdk8, 1009 is the minimum value that StringTable can set.

12.2 memory allocation of string

  • jdk6 and previous string constant pools are stored in the permanent generation. jdk7 adjusts the position of the string constant pool to the java heap. jdk8 string constant pool is in heap.

12.3 string splicing

  • The splicing result of constants and constants is in the constant pool. The principle is compile time optimization

    String s1 = "a" +"b" +"c";//Equivalent to "abc", which is assigned to "abc" during compilation
    String s2 = "abc";
    System.out.println(s1 == s2);//true
    System.out.println(s1.equals(s2));//true
    
  • Constants with the same content will not exist in the constant pool.

  • As long as one of them is a variable, the result is in the heap. The principle of variable splicing is stringBuilder

  • If the result of splicing calls the intern() method, the string object not yet in the constant pool will be actively put into the pool and the object address will be returned.

String s1 = "javaEE";
String s2 = "hadoop";

String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";//Compiler optimization 
//If variables appear before and after the splicing symbol, it is equivalent to new String() in the push space
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
//jdk5. After 0, execute the details: first, new StringBuilder, then append(s1) and append(s2) respectively, and finally toString() generates a string object
String s7 = s1 + s2;

System.out.println(s3 == s4);//true
System.out.println(s3 == s5);//false
System.out.println(s3 == s6);//false
System.out.println(s3 == s7);//false
System.out.println(s5 == s6);//false
System.out.println(s5 == s7);//false
System.out.println(s6 == s7);//false
//Verify that if the value of s6 exists in the string constant pool, the address is returned to s8. If not, the string is created in the constant pool.
String s8 = s6.intern();
System.out.println(s3 == s8);//true

----------------------------------------------------------------------------------------
//String splicing does not necessarily use StringBuilder, but may also be compiler optimization
final String s1 = "a";//constant
final String s2 = "b";//constant
String s3 = "ab";
String s4 =  s1 + s2;
System.out.println(s3 == s4);//true

-----------------------------------------------------------------------------------------
When string splicing is performed many times, StringBuilder of append()The efficiency is much higher than+Operation.
StringBuilder of append Only one has been created from beginning to end StringBuilder Object. and String Multiple splices have been created StringBuilder and String If the memory consumption of the object is large GC,It also takes time.
Optimization space: if the length of the string is basically determined, the capacity of the (underlying array) can be specified when creating an object to avoid multiple subsequent creation StringBuilder. Namely StringBuilder sb = new StringBuilder(capacity);

12.4 use of intern()

  • If it is not a string object declared in double quotation marks, you can use the intern method provided by string: the intern method will query whether the current string exists from the string constant pool. If it does not exist, it will put the current string into the constant pool.
    For example: string myinfo = new string ("I love you") intern();
  • Ensure that strings with the same content will not be stored in the string constant pool

Ensure that the variable s points to the data in the string constant pool in the following ways

  1. String s = "I Love com";// Definition of literal quantity
  2. String s = ......intern();// Call the intern() method

12.5 new String()

new String("ab"); How many objects will be created? Two, ldc through bytecode instruction

: an object is created in the heap with the new keyword, and an object is created in the string constant pool

String str = new String("a") + new String("b"); How many objects will be created?

  • Object 1: new StringBuilder()

  • Object 2: new String("a");

  • Object 3: "a" in string constant pool

  • Object 4: new String("b")

  • Object 5: "b" in string constant pool

  • In depth analysis: toString() of StringBuilder;

    Object 6: toString() will new String("ab");

    There is no 'ab' in the string constant pool

String s1 = new String("1");//s points to "1" in the queue. At this time, there is also "1" in the constant pool
s.intern();
String s2 = "1";//Literal definition "1" refers to the existing "1" in the constant pool
System.out.println(s1 == s2);//jdk6:false jdk7/8:false

String s3 = new String("1") + new String("1");//s3 refers to the new String("11") in the heap. At this time, there is no "11" in the constant pool
s3.intern();//Open up space in the string constant pool and store the address of new String("11") in the above heap
String s4 = "11";//s4 points to "11" in the constant pool and actually points to the address of "11" in the heap
System.out.println(s3 == s4);//jdk6:false jdk7/8:true
  • Summarize the use of string intern():
    jdk1.6, try to put this string object into the string pool.

    • If there is in the string pool, it will not be put in. Returns the address of the object in the existing string pool

    • If not, a copy of this object will be copied and put into the string pool, and the address of the object in the string pool will be returned

    Jdk1. From 7, try to put this string object into the string pool.

    • If there is in the string pool, it will not be put in. Returns the address of the object in the existing string pool
    • If not, it will copy the reference address of the object, put it into the string pool, and return the reference address in the string pool

Example:

String s = new String("a") + new String("b");
String s2 = s.intern();
System.out.println(s2 == "ab");
System.out.println(s == "ab");


Explain why jdk7/8 All of them true?: 
String s = new String("a") + new String("b");//
String s2 = s.intern();//The address of new String("ab") in the heap is stored in the string constant pool
System.out.println(s2 == "ab");//JDK6: true JDK7 / 8: true -- > compared with "ab", it is found that there is "ab" in the heap, so the equal sign points to "ab" in the heap
System.out.println(s == "ab");//jdk6:false jdk7/8:true

Tags: Java jvm string

Posted by andremta on Mon, 09 May 2022 10:11:02 +0300