Java String: String Constant Pooling As the most basic reference data type, Java designers have provided String Constant Pooling for strings to improve their performance. What is the principle behind String Constant Pooling?

What is the design philosophy behind the string constant pool?

Where is the string constant pool?

How do I manipulate string constant pools?

String allocation, like other object allocation, costs a lot of time and space. As the most basic data type, a large number of frequently created strings greatly affects the performance of the program

In order to improve performance and reduce memory overhead, the JVM has made some optimizations when instantiating string constants

Create a pool of string constants for strings, similar to a cache

When creating a string constant, first insist on whether the string exists in the string constant pool, then return a reference instance, then instantiate the string and place it in the pool

Fundamentals of Implementation

The basis for this optimization is that strings are immutable and can be shared without worrying about data conflicts

The global string constant pool created by the runtime instance has a table that always maintains a reference for each unique string object in the pool. This means that they always refer to objects in the string constant pool, so the strings in the constant pool are not collected by the garbage collector

Code: Gets the corresponding string from the string constant pool

Strings str1 = “hello”; String str2 = “hello”;

System.out.printl(”str1 == str2″ : str1 == str2 ) //true

When analyzing the location of the string constant pool, we first look at the heap, stack, and method area:

The heap stores objects, each of which contains a corresponding class

There is only one heap (heap) in the JVM that is shared by all threads, and there is no base type or object reference in the heap, only the object itself

Objects are collected by the garbage collector, so the size and life cycle do not need to be determined

The stack

Each thread contains a stack that holds only references to objects of basic data types and custom objects (not objects)

The data in each stack (primitive types and object references) is private

The stack is divided into three parts: the basic type variable area, the execution context, and the operation instruction area (to store the operation instruction).

The data size and life cycle are determinable, and when there are no references to the data, the data automatically disappears

Methods area

Static regions, like heaps, are shared by all threads

The method area contains elements that are always unique in the entire program, such as class and static variables

The string constant pool exists in the method area

Code: Stack method area stores strings

Strings str1 = “ABC”; String str2 = “ABC”; String str3 = “ABC”; String str4 = new String(” ABC “); String str5 = new String(” ABC “);

String object creation

String str4 = new String(” ABC “) How many objects are created?

Look for an “ABC” object in the constant pool

If so, the corresponding reference instance is returned

If not, a corresponding instance object is created

New String(” ABC “) in the heap

Assign the object address to str4, creating a reference

So, if there is no “ABC” literal in the constant pool, two objects are created, otherwise one object is created, and a reference is created

(2) I’m sorry. (3) I’m sorry. (4)

String str1 = new String(“A”+”B”) ; How many objects will be created? String str2 = new String(“ABC”) + “ABC” ; How many objects will be created?

Str1: String constant pool: “A”,”B”,”AB” : 3 heaps: new String(“AB”) : 1 reference: str1:1 total: 5

Str2: String constant pool: “ABC” : 1 heap: new String(“ABC”) : 1 reference: str2:1 total: 3

Code: Variables and constants of the base type, variables and references are stored in the stack, and constants are stored in the constant pool

int a1 = 1;

int a2 = 1;

int a3 = 1;

public static int INT1 =1 ;

public static int INT2 =1 ;

public static int INT3 =1 ;

How to manipulate a pool of string constants

When the JVM instantiates a string constant pool

Strings str1 = “hello”; String str2 = “hello”;

System.out.printl(”str1 == str2″ : str1 == str2 ) //true

String.intern()

When the intern() method is called, the compiler adds the string to the pool of constants (maintained by StringTable) and returns a reference to that constant.



// Create three strings in three different ways.

String s1 = “Hello”;

String s2 = new StringBuffer(“He”).append(“llo”).toString();

String s3 = s2.intern();

// Determine which strings are equivalent using the ==

// operator

System.out.println(“s1 == s2? ” + (s1 == s2)); // false

System.out.println(“s1 == s3? ” + (s1 == s3)); // true

Literals can be understood as actual values. 8 in int a = 8 and hello in String a = “hello” are literals

Symbolic reference is a string, as long as we in the code references an non literal, whether it is a variable or constant, it is defined by a string of symbols, the string in the constant pool, class loading when loaded into this symbol for the first time, will be the symbol references (string) into direct reference (pointer)

String objects are stored internally with character arrays, so look at the following example:

String m = “hello,world”; String n = “hello,world”; String u = new String(m); String v = new String(“hello,world”); A char array of 11 length is allocated, and a string of the char array is allocated in the constant pool, and the string is referenced by m

The string in the constant pool is referred to by n, so it refers to the same object as m

Generates a new string, but the inner character array refers to the inner character array of M

A new string is generated, but the inner character array refers to the inner character array of the string in the constant pool, meaning that it is the same character array as u

Using a graph, it would look something like this (the dotted line just means there is no special relationship between the two):

Test the demo:

String m = “hello,world”;

String n = “hello,world”;

String u = new String(m);

String v = new String(“hello,world”);

System.out.println(m == n); //true

System.out.println(m == u); //false

System.out.println(m == v); //false

System.out.println(u == v); //false

Conclusion:

M and N are the same object

M, U, V are all different objects

M, N, U, V all use the same array of characters, and equal will return true