Explore Java constant pools in depth

Constant pools in Java are usually divided into two types: static constant pools and runtime constant pools

Static constant pool: the constant pool in the class file. The constant pool in the class file includes string (number) literal value, class and method information, which occupies most of the space of the class file.

Runtime constant pool: after loading the class, the JVM loads the constant pool in the class file into memory and saves it in the method area. The constant pool we usually talk about refers to the runtime constant pool in the method area. Another important feature relative to the constant pool of the class file is that it is dynamic. The Java language does not require that constants must be generated only during compilation, that is, the contents of the constant pool not preset in the class file can enter the runtime constant pool of the method area, and new constants may also be put into the pool during operation, The intern () method of the string class is used by developers more often.

Program counter: it is the pipeline of program execution, indicating which command to execute next.

Local method stack: the stack used by the JVM to call operating system methods.

Virtual machine stack: the stack used by the JVM to execute java code

Virtual machine heap: where objects are stored. In Java programs, new objects are stored in the heap.

Method area: it stores constants, class information and static variables, which can be understood as the location of class files in memory.

Benefits of constant pool:

Constant pool is to avoid frequent creation and destruction of objects, which will affect the system performance. It realizes the sharing of objects.

For example, in the string constant pool, all string literals are put into a constant pool at the compilation stage.

翻译错误 TIMEOUT

2. Save running time: when comparing strings, = = is faster than equals(). For two reference variables, only = = is used to judge whether the references are equal, and you can also judge whether the actual values are equal.

==The meaning of basic data type and object representation is different.

For the basic data type: = = the value of the basic data type is compared. For the object: = = the memory address of the object in memory is compared

Wrapper classes and constant pools of 8 basic data types

Most of the wrapper classes of basic data types in Java implement constant pool technology, that is, byte, short, integer, long, character and Boolean.

The five wrapper classes byte, short, integer, long and character create [- 128127] corresponding types of cache data by default and store them in the constant pool. Beyond this range, new objects will still be created.

2. The two floating-point wrapper classes float and double do not implement constant pool technology

3. Scenario of applying constant pool

(1) . integeri1 = 40; because integer is a wrapper class of the basic data type int and an object, Java will perform automatic boxing operation during compilation, and directly encapsulate the code into integeri1 = integer. Valueof (40), so as to use the objects in the constant pool

(2).Integeri1=newInteger(40); In this case, a new object is created

In this case, new integer will not perform auto boxing to reference the existing constants in the constant pool, but will directly generate a new object in the heap.

4. Detailed explanation of integer

Explanation: statement I4 = = i5 + I6, because the + operator is not applicable to integer objects. First, i5 and I6 automatically unpack and add values, that is, I4 = = 40. Then, the integer object cannot be directly compared with the value, so I4 automatically unpacks to the int value of 40, and finally this statement is converted to 40 = = 40 for numerical comparison.

String class and constant pool

1. Creation method of string object

The two different creation methods are different. The first is to get objects in the constant pool, and the second is to create new objects in the heap memory space.

Just use new to create a new object in the heap.

2. Connection expression+

(1). Only new objects generated by "+" connection between string objects created by using the method of "" containing text will be added to the string constant pool.

(2). For other forms, such as direct "+" connection between two object references or object connection created through new, the generated new object will not be added to the string constant pool.

Both a and B are constants, and the value is fixed, so the value of S is also fixed, which has been determined when the class is compiled. That is: String s = a + B; Equivalent to: String s = "ab" + "CD";

Although a and B are defined as constants, they are not assigned immediately. Before calculating the value of S, when they are assigned and what value they are assigned are variables. Therefore, the properties of a and B are similar to a variable before they are assigned. Then s cannot be determined at compile time, but can only be created at run time.

3.Strings1=newString(“xyz”); How many objects were created?

Consider the class loading phase and actual execution time.

(1) Class loading will only be performed once for a class. "XYZ" has been created and resident when the class is loaded (if the "XYZ" string has been resident before the class is loaded, there is no need to create a "XYZ" instance for resident repeatedly). The resident string is placed in the globally shared string constant pool.

(2) When this code is subsequently run, the string instance corresponding to the literal "XYZ" has been fixed and will not be created repeatedly. Therefore, this code copies an object in the constant pool into the heap and gives the reference of this object in the heap to S1.

This statement creates 2 objects.

4.java. lang.String. intern()

Another important feature of the runtime constant pool relative to the constant pool of the class file is that it is dynamic. The Java language does not require that constants must be generated only during compilation, that is, the contents of the constant pool not preset in the class file can enter the runtime constant pool of the method area, and new constants may also be put into the pool during operation, The intern () method of the string class is used by developers more often.

String's intern() method will find out whether there is an equal string in the constant pool. If so, it will return the reference of the string. If not, it will add its own string into the constant pool.

summary

The above is all about the in-depth exploration of Java constant pool in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>