1. The concept

To start with a review of the Java memory model, the Java runtime data area is roughly divided into five blocks

  • Methods area
  • The virtual machine stack
  • Local method stack
  • The heap
  • Program counter

The runtime constant pool is part of the method area.

The Runtime Constant Pool is part of the method area. In addition to the Class version, field, method, and interface descriptions, the Class file contains the ConstantPool Table, which is used to store the various literals and symbolic references generated by the compiler. This part of the Table is stored in the runtime ConstantPool of the method area after the Class is loaded.

The runtime constant pool contains data loaded from the constant pool table in the Class file. To find out what is in the runtime constant pool table, we need to ask what is in the corresponding constant pool table

2. Constant pool table

2.1 Data type of the Class file

The file format of the Class file uses a pseudo-structure similar to the C-language structure to store data. There are only two types of data in the pseudo-structure, “unsigned number” and “table.”

  • Unsigned numbers are basic data types. U1 / U2 / U4 / U8 represent unsigned numbers of 1 byte, 2 byte, 4 byte, and 8 byte respectively. Unsigned numbers can be used to describe numbers, index references, quantity values, or string values encoded in UTF-8.
  • A table is a data type that consists of multiple unsigned numbers or other tables as data items. To facilitate differentiation, all table names are customarily terminated with “_info”. Tables are used to describe hierarchical composite structures of data, and the entire Class file can be viewed as essentially a table.

2.2 constant pool

A constant pool can be likened to a repository of resources in a Class file. It is the data that is most associated with other items in the Class file structure.

The constant pool contains two main pieces of data: literals and symbolic references. Literals are close to the Java language level concepts of constants, such as text strings, constant values declared final, and so on.

2.2.1 Symbolic Reference

Since Java code does not “wire” during Javac compilation as C/C++ does, it is dynamically wired when the Clsss file is loaded by the VIRTUAL machine, the Class file does not hold the in-memory layout of methods, fields, and so on after we compile Java code into a Class file. To solve this problem, Class files hold symbolic references to methods, fields, and so on in the constant pool. Symbolic references are simply placeholders for real memory layouts that are replaced by real direct references during the parsing phase of the class loading process.

2.2.2 Structure of constant pool

Each constant in the constant pool is a table. These tables have a common feature, that is, the first bit of the table structure starts with a flag bit of type U1, which represents which constant type the current constant belongs to

The item type in the constant pool

project type describe
CONSTANT_Utf8_info 1 The character string is utF-8 encoded
CONSTANT_Integer_info 3 Integer literal
CONSTANT_Float_info 4 Floating point literals
CONSTANT_Long_info 5 Long integer literals
CONSTANT_Double_info 6 A double – precision floating-point literal
CONSTANT_Class_info 7 Symbolic reference to a class or interface
CONSTANT_String_info 8 String type literals
CONSTANT_Fieldref_info 9 Symbolic reference to a field
CONSTANT_Methodref_info 10 Symbolic references to methods in a class
CONSTANT_InterfaceMethodref_info 11 Symbolic references to methods in the interface
CONSTANT_NameAndType_info 12 A partial symbolic reference to a field or method
CONSTANT_MethodHandle_info 15 Represents a method handle
CONSTANT_MethodType_info 16 Presentation method type
CONSTANT_Dynamic_info 17 Represents a dynamically computed constant
CONSTANT_InvkoeDynamic_info 18 Represents a dynamic method call point
CONSTANT_Module_info 19 Represents a module
CONSTANT_Package_info 20 Represents open or exported packages in a module

Table information and table structure are as follows:

2.2.2.1 CONSTANT_Utf8_info

type mark describe
tag u1 Value of 1
length u2 Number of bytes in a UTF-8 encoded string
bytes u1 The length of the string is utF-8. The length is a total of length

2.2.2.2 CONSTANT_Integer_info

project type describe
tag u1 A value of 3
bytes u4 Int values stored first in order of high order

2.2.2.3 CONSTANT_Float_info

project type describe
tag u1 A value of 4
bytes u4 Float values stored first in order of magnitude

2.2.2.4 CONSTANT_Long_info

project type describe
tag u1 A value of 5
bytes u8 The long value stored in front of the high order

2.2.2.5 CONSTANT_Double_info

project type describe
tag u1 A value of 6
bytes u8 A double stored in front of the high order

2.2.2.6 CONSTANT_Class_info

project type describe
tag u1 A value of 7
index u2 Index to a fully qualified named constant item

2.2.2.7 CONSTANT_String_info

project type describe
tag u1 A value of eight
index u2 Index to a string literal

2.2.2.8 CONSTANT_Fieldref_info

project type describe
tag u1 A value of 9
index u2 Index entry to the class or interface descriptor CONSTANT_Class_info that declares the field
index u2 Index entry pointing to the field descriptor CONSTANT_NameAndType

2.2.2.9 CONSTANT_Methodref_info

project type describe
tag u1 A value of 10
index u2 Index entry to the class or interface descriptor CONSTANT_Class that declares the method
index u2 Index entry pointing to the name and type descriptor CONSTANT_NameAndType

2.2.2.10 CONSTANT_InterfaceMethodref_info

project type describe
tag u1 A value of 11
index u2 The index entry pointing to the interface descriptor CONSTANT_Class for the declared method
index u2 Index entry pointing to the name and type descriptor CONSTANT_NameAndType

2.2.2.11 CONSTANT_NameAndType_info

project type describe
tag u1 A value of 12
index u2 Index to the constant entry of the field or method name
index u2 The index pointing to the constant entry of the field or method descriptor

2.2.2.12 CONSTANT_MethodHandle_info

project type describe
tag u1 A value of 15
reference_kind u1 The value must be between 1 and 9 [1-9] which determines the type of method handle. The value of the method handle type represents the bytecode behavior of the method handle
reference_index u2 The value must be a valid index for open eating

2.2.2.13 CONSTANT_MethodType_info

project type describe
tag u1 A value of 16
descriptor_index u2 The value must be a valid index to the constant pool, and the constant pool entry at that index must be a CONSTANT_Utf8_info structure representing the descriptor of the method

2.2.2.14 CONSTANT_Dynamic_info

project type describe
tag u1 A value of 17
bootstrap_method_attr_index u2 The value must be a valid index to the bootSTRAP_methods [] array of the bootstrap method table in the current Class file
name_and_type_index u2 The value must be a valid index to the current constant pool, and the constant pool entry at that index must be a CONSTANT_NameAndType_info structure representing the method name and method descriptor

2.2.2.15 CONSTANT_InvkoeDynamic_info

project type describe
tag u1 A value of 18
bootstrap_method_attr_index u2 The value must be a valid index to the bootSTRAP_methods [] array of the bootstrap method table in the current Class file
name_and_type_index u2 The value must be a valid index to the current constant pool, and the constant pool entry at that index must be a CONSTANT_NameAndType_info structure representing the method name and method descriptor

2.2.2.16 CONSTANT_Module_info

project type describe
tag u1 A value of 19
name_index u2 The value must be a valid index to the constant pool, and the constant pool entry at that index must be a CONSTANT_Utf8_info structure representing the module name

2.2.2.17 CONSTANT_Package_info

project type describe
tag u1 A value of 19
name_index u2 The value must be a valid index to the constant pool, and the constant pool entry at that index must be a CONSTANT_Utf8_info structure representing the package name

3. View the constant pool table

If we use a text editor to open a Class file, you might see something like this:

Other than the magic number CAFE BABE information can be quite difficult to read. Fortunately, Oracle provides a tool for analyzing Class files bytecode: Javap. How to use Javap:

javap -verbose xxxx.class
Copy the code

Write a Java class Test as follows:

public class Test {
    String test = "dafa";
    String test1 = "soft";
    String test3 = "dafasoft";

    public Test(a) {}void Test(a) {
        this.testFun();
    }

    public void testFun(a) {}public void testFun1(a) {}public void testFun2(a) {}public static void main(String[] args) {}}Copy the code

Let’s compile and use the Javap command to see what its constant pool table looks like. A screenshot of its constant pool table is shown below:

The Javap tool annotates the table for us automatically, but it actually makes sense if we look through the above table structure without comments. Here are two examples:

3.1. Example 1: String test3 = “dafasoft”;

This is a literal string constant defined in our Java class. How do we find it in the constant pool table? First of all, it is a Field. We first look for fieldrefs. There are three fieldrefs in the table above.

According to the table structure in section 2.2.2.8, the two values after Filedref are two indexes. An index to the class or interface descriptor CONSTANT_Class_info for the declared field and an index to the field descriptor CONSTANT_NameAndType, such as Fieldref at #7 with values #9.#40 and #9 with values #42. #42 The corresponding value string “com/dafasoft/test/ test”; The value of #40 is #14:#12, the value of #14 is the String test3, the value of #12 is the String “Ljava/lang/String; Together, the information channel, we can come to # 7 in the Fieldref is pointing to the com/dafasoft/test/test. The test3: Ljava/lang/String; A character reference to.

Up to now, we already know in the class has a reference point to com/dafasoft/test/test. The class variables test3, its type is String type, so how it is and its value “dafasoft” link? This is related to the initialization of the object, and the specific bytecode is in the method:

Notice lines 17 and 19 of the Code. LDC = ‘push int, float, String from constant pool to top of stack’ putfied = ‘assign field to instance of specified class’ After executing these two instructions, we are finished assigning the String variable test3.

3.2 Example 2: this.testfun ();

This statement is in the constructor Test(). To call a method, you need to know the reference to the method in the method area. Method references in the constant pool table are of the formCONSTANT_Methodref_infoIn this example, the constant pool table has index #8, which we will not expand. It is the same as the constant pool table in example 1com/dafasoft/test/Test.testFun:()VA symbolic reference to the test class in the com.dafasoft. Test packagevoid testFun()Methods. The call is in the bytecode of the Test method:

Look for the Code of the first line, the line instruction mean, call to character reference # 8 method, the character reference # 8 corresponding method is com/dafasoft/test/test testFun (V)

From this we can also conclude that a reference to a method is added to the constant pool only when a method call is made in a Java class. So where do the methods defined in this class go? For example, if the Test class has three methods, only testFun is called, and references to testFun1 and testFun2 do not appear in the constant pool table. So where are the implementations of these three methods? The answer is in the bytecode method table, which is a topic for another day.

There are 17 types in the constant pool table, and references to all 17 types can be derived in this way, so we won’t go through them all.