Java Virtual Machine: runtime volume pool, Java Virtual Machine volume pool
1. Introduction to the runtime frequent pool
Runtime Constant Pool, which is part of the method area. In addition to the description of the version, field, method, and interface of the Class, the Class file also contains the Constant Pool Table ), it is used to store various literal and symbolic references generated during the compilation period. This part of content will be stored in the constant pool after the class is loaded.
The runtime constant is relative to the constant. It has an important feature: dynamic. Of course, the dynamic constants with the same values are different from the constants we usually call for, but they are all stored in the same memory area of the pool. The Java language does not require constants to be generated only during the compilation period. New constants may also be generated during the runtime. These constants are placed in the runtime pool. Constants mentioned here include: basic type packaging class (Packaging class does not manage floating-point type, and integer type only manages-128 to 127) and String (you can also use String. the intern () method can forcibly put String into the constant pool)
II. Information constant pool in the Class file
In the Class file structure, the first four bytes are used to store the Megic Number, to determine whether a file can be accepted by JVM, and then four bytes are used to store the version Number, the first two bytes store the version number, the last two store master versions, and then the constant pool used to store constants. because the number of constants is not fixed, therefore, the entry of the constant pool stores a data of the U2 type (constant_pool_count) to store the capacity Count value of the constant pool.
Constant pools are mainly used to store two types of constants: Literal and Symbolic References. Literally, they are equivalent to Java-level constants, such as text strings, when declared as a final constant value, symbolic reference is a concept in terms of compilation principles, including the following three types of constants:
- Fully qualified names of classes and interfaces
- Field name and Descriptor
- Method Name and Descriptor
3. Benefits of constant pool
Constant pool is used to avoid frequent creation and destruction of objects and affect system performance. It shares objects. For example, in the String constant pool, all the strings are put in a constant pool during compilation.
- Memory saving space: all the same string constants in the constant pool are merged, occupying only one space.
- Save running time: when comparing strings, = is faster than equals. For two referenced variables, you can only use = to determine whether the referenced variables are equal, so that you can determine whether the actual values are equal.
Meaning of equal sign =
- The two equal signs are used between basic data types to compare their values.
- The dual-equal signs are used between the composite data types (classes) to compare their storage addresses in the memory.
Iv. Basic types of packaging class and constant pool
In java, most of the basic types of packaging classes implement the constant pool technology, that is, Byte, Short, Integer, Long, Character, Boolean. By default, these five packaging classes create cache data of the corresponding type of value [-128,127], but new objects will still be created beyond this range. The packaging classes Float and Double of the two floating point number types do not implement the constant pool technology.
1) Integer and constant pool
Integer i1 = 40;Integer i2 = 40;Integer i3 = 0;Integer i4 = new Integer(40);Integer i5 = new Integer(40);Integer i6 = new Integer(0);System.out.println("i1=i2 " + (i1 == i2));System.out.println("i1=i2+i3 " + (i1 == i2 + i3));System.out.println("i1=i4 " + (i1 == i4));System.out.println("i4=i5 " + (i4 == i5));System.out.println("i4=i5+i6 " + (i4 == i5 + i6)); System.out.println("40=i5+i6 " + (40 == i5 + i6));i1=i2 truei1=i2+i3 truei1=i4 falsei4=i5 falsei4=i5+i6 true40=i5+i6 true
Explanation:
- Integer i1 = 40; Java will directly encapsulate the code into Integer i1 = Integer. valueOf (40); during compilation, so as to use the objects in the constant pool.
- Integer i1 = new Integer (40); in this case, a new object is created.
- Statement i4 = i5 + i6, because the + operator is not applicable to Integer objects, the i5 and i6 perform the automatic unpacking operation to add the values, that is, i4 = 40. Then the Integer object cannot be directly compared with the value, so i4 Automatically splits the box into an int value of 40, and finally converts this statement to a value of 40 = 40 for comparison.
2) String and constant pool-normal method assignment
String str1 = "abcd";String str2 = new String("abcd");System.out.println(str1==str2);//falseString str1 = "str";String str2 = "ing";String str3 = "str" + "ing";String str4 = str1 + str2;
System.out.println("string" == "str" + "ing");// trueSystem.out.println(str3 == str4);//falseString str5 = "string";System.out.println(str3 == str5);//true
Explanation:
- "Abcd" is an object in the constant pool, and new String ("abcd") is a new object directly created in the heap memory space. You only need to use the new method to create a new object.
- Join expression +. Only new objects created by concatenating String objects created by using quotation marks and text are added to the constant pool.
- For the "+" join expression of the string variable, all the new objects it generates will not be added to the string pool. It belongs to the string created at runtime and has an independent memory address, therefore, it does not reference the same String object.
3) String and constant pool-static method assignment
Public static final String A; // constant Apublic static final String B; // constant Bstatic {A = "AB"; B = "cd ";} public static void main (String [] args) {// concatenates two constants with + to initialize s. String s = A + B; String t = "abcd "; if (s = t) {System. out. println ("s equals t, they are the same object");} else {System. out. println ("s is not equal to t, they are not the same object ");}}
Explanation:
S is not equal to t. They are not the same object. Although A and B are defined as constants, they are not immediately assigned values. Before calculating values of s, it is a variable to determine when values are assigned and what values are assigned. Therefore, before being assigned A value, A and B are similar to A variable. S cannot be determined during the compilation period, but can only be created at runtime.
4) String and constant pool-intern Method
Public static void main (String [] args) {String s1 = new String ("computer"); String s2 = s1.intern (); String s3 = "computer"; System. out. println ("s1 = s2? "+ (S1 = s2); System. out. println (" s3 = s2? "+ (S3 = s2 ));}
S1 = s2? Falses3 = s2? True
Explanation:
The intern () method of String searches for whether a equal String exists in the constant pool. If yes, it returns a reference to the String. If no, it adds its own String to the constant pool.
5) String and constant pool-extend
String s1 = new String ("xyz"); // how many objects are created?
Explanation:
Consider the class loading stage and actual execution time.
- The class is loaded only once for a class ." Xyz "is created and resident when the class is loaded. (If the" xyz "string already exists before the class is loaded, you do not need to create the" xyz "instance for Resident again). The resident string is placed in the global shared String constant pool.
- When this code is subsequently executed, the String instance corresponding to the "xyz" literal has been fixed and will not be created again. So this code copies the objects in the constant pool to heap, and transfers the reference of this object in heap to s1.
This statement creates two objects.