1. Basic Concepts:

1.1. Constants:

  • Constants are final modified variables or strings defined at compile time.
  • Constants are loaded into the class’s constant pool at compile time.

1.2. What are literal and symbolic references?

  • Literals include: 1. Text strings; 2. Values of the eight basic types; 3.
  • Symbolic references include:
    • A set of symbols describing the referenced object, which can be any literal, as long as it is used unambiguously to locate the object.
    • It is distinguished from a direct reference, which is typically a local pointer to a method area.
    • There are three types of constants: 1. Fully qualified names of classes and methods 2. Names and descriptors of fields 3. The name and descriptor of the method.
    • When the virtual machine is running, the corresponding symbolic quotes need to be retrieved from the constant pool and parsed and translated into a specific memory address during class creation or runtime (direct reference).

1.3 Benefits of constant Pool:

  • Constant pool is to avoid frequent creation and destruction of objects that affect system performance, and it realizes object sharing.
  • For example, string constant pool, which puts all string literals into one constant pool at compile time:
    • Memory saving: All the same string constants in the constant pool are merged and occupy only one space.
    • Save runtime: When comparing strings,= =thanequals()Fast. For two reference variables, only= =By determining whether references are equal, you can also determine whether the actual values are equal.

Static Constant Pool (also called class Constant Pool, class Constant Pool)

  • *. Class file constant pool, each Java class we write is compiled into a class file; The class file contains the constant pool table, which is used to store all kinds of Literal and Symbolic References generated by the compiler. The constant pool table contains all kinds of Literal and Symbolic References generated by the compiler. Takes up most of the space in the class file. (Compile time)
  • Each class file has a class constant pool.

Runtime Constant Pool

  • After class loading, the JVM loads the constant pool from the class file into memory and stores it in the method area. (Operating period)
  • The runtime constant pool exists in the method area of JVM memory and is used to store literal and symbolic references generated during compilation.
  • When a class is loaded into JVM memory, the JVM stores the contents of the class constant pool into the runtime constant pool of the method area, which is also one for each class.
  • The Java language does not require constants to be generated only at compile time. That is, constants that are not in the constant pool of the Class file can be added to the method area runtime constant pool. New constants can also be added to the runtime constant pool at run time, such as the Intern () method of the String Class.
  • In the class parsing phase, symbolic references are replaced with direct references, and the parsing process queries the string constant pool, known as StringTable, to ensure that the strings referenced by the constant pool at run time are consistent with those in the string constant pool.
  • OutofMemoryError is also raised when the constant pool can no longer allocate memory.

4, string constant pool: [belongs to runtime constant pool, only stores strings]

4.1 Where is the string constant pool in the Java memory region:

  • Prior to JDK7, the string constant pool was in the method area, but since JDK7, the Hotspot VIRTUAL machine has moved the string constant pool that was in the permanent generation to the heap.

What is a string constant pool?

  • The string pool implemented in HotSpot is a fixed size HashTable, which is a Hash table with a default size of 1009; This StringTable has only one copy per HotSpot VM instance, shared by all classes. String constants are made up of one character at a time, placed on a StringTable that holds key-value pairs (the literal “ABC”, that is, the resident string).
  • In JDK6.0, stringTables have a fixed length of 1009, so if you put too many strings into a String Pool, you’ll have hash collisions and the list will be too long. When you call String#intern(), you’ll need to search the list one by one. Resulting in a significant performance decline;
  • In JDK7.0, the length of a StringTable can be specified by the following parameter: -xx :StringTableSize=66666

4.3 what is in the string constant pool?

  • In JDK6.0 and earlier, String pools are String constants;
  • In JDK7.0, because String#intern() has been changed, String pools can also hold reference addresses for String objects placed in the heap.
  • Note: there is only one string in the string constant pool!
    String s1 = "hello,world!";
    String s2 = "hello,world!";
	// After the first line of code is executed, "Hello,world! , s2 does not apply for new space in the constant pool, but directly returns the existing string memory address to S2.
Copy the code

String a= new String(“ha”);

  • The directly defined “HA” is stored in the string constant pool; New String(” ha “) is String stored in the heap, “ha” in the String constant pool;
  • There is only one of the same strings in the constant pool, but each new object in a new String(” ha “) creates a new object in the heap, regardless of whether the value is the same;
  • String a= “ha” and String b = “ha” : both a and b refer to “ha” in the String constant pool, so a==b;
  • String a = new String(” ha “) and String b = new String(” ha “) : yes, two objects are created in the heap, and both objects are ha, so a! = b; A.e quals (b) returns true;
  • String a = “ha” is created in memory at compile time, String a = new String(” HA “) is created in the heap at run time.

4.5 String Concatenation

Create with "" double quotes: String s1 =" first "; String s2= "se" + "cond"; String s12= "first" +s2; usenewString(" ") created: String s3 =newString (" three "); usenewString(" ") splicing: String s4 =newString (" fo ") + "ur"; usenewString(" ") splicing: String s5 =new String(“fo”)+newString (" ur "); The "first" in s1: is a string constant, which is determined at compile time. First is checked to see if the string constant pool contains the "first" string. If not, add "first" to the string constant pool and point to it directly. So s1 points directly to the "first" object in the string constant pool. S2: "se" and "cond" are also string constants. When a string is concatenated from multiple string constants, it must be a string constant itself, so S2 is also parsed as a string constant at compile time, and s2 is a reference to "second" in the constant pool. S12: JVM for string references, because string references exist in the string "+" concatenation, and the value of the reference is not determined at compile time, i.e."first"+s2) cannot be optimized by the compiler, and only dynamically allocates new strings to S12 at run time using StringBuilder connections. The compiler creates a StringBuilder object, calls append(), and finally toString() to create a new String containing the modified String contentsnewThe String created by String() is not a constant and cannot be determined at compile time, sonewStrings created by String() do not go into the constant pool; they have their own address space. But the String constant "three" is also added to the String constant pool at compile time (if it doesn't already exist) s4: Again not determined at compile time, but the String constants "fo" and "ur" are also added to the String constant pool and String objects are created in the heap. (The string constant pool does not store the string "four", it stores the string "fo")Copy the code

4.6 Final Reference Splicing:

	public class StringConcat {
	    final String a = "hello";
	    final String b = "moto";
	    String result = a + b + "2018"; }, forfinalA local variable that is resolved at compile time to a local copy of a constant value stored in its own constant pool or embedded in its bytecode stream. • So (a + b + ")2018") and (" Hello "+" Moto "+"2018") the effect is the same.Copy the code

4.7, Intern () Method:

  • String.intern() is a Native method that returns a String representing the String in the String constant pool if the String constant pool already contains a String equal to the String. Not string objects in the heap, but string objects in the string constant pool. Otherwise, the String reference address (in the heap) is added to the String constant pool. Since the String constant pool is in the heap, there is no need to create another String in the String constant pool.
  • The string constant pool after JDK 1.7 exists in the heap.
String aa=new String("abcdef");//"abcdef" string object, created in the string constant pool
String aaIntern=aa.intern();//aaIntern is the "abcdef" object of the string constant pool
System.out.println("aa==aaIntern "+(aa==aaIntern));//false   
String aaStr="abcdef";
System.out.println("aaIntern==aaStr "+(aaIntern==aaStr));//true
System.out.println("aa==aaStr "+(aa==aaStr));/ / false aa in the heap, aaStr in string constant pool -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
String bb = new String("123") + "456";
String bbIntern = bb.intern();
System.out.println("bb==bbIntern " + (bb == bbIntern));// true, string constant pool does not have "123456"
String bbStr = "123456";
System.out.println("bb==bbStr " + (bb == bbStr));// true
System.out.println("bbIntern==bbStr " + (bbIntern == bbStr));// true
String cc = new String("1") + "23";
String ccIntern = cc.intern();// String constant pool already has "123"
System.out.println("cc==ccIntern " + (cc == ccIntern));// false
Copy the code

Reference: tech.meituan.com/2014/03/06/…