Introduction to the

The JVM loads, links, and initializes class files at runtime. What magic happens to the constant pool defined in the class file after the JVM loads? Take a look.

Constant pool in the class file

When we talked about the structure of a class file, we mentioned that every class file has a constant pool. What is stored in the constant pool?

String constants, class and interface names, field names, and other constants referenced in class.

Run-time constant pool

But the constant pool in the class file is definitely not enough, because we need to run it in the JVM.

A pool of runtime constants is needed to run the JVM.

The runtime constant pool corresponds one-to-one to the constant pool of the class file, which is built from the constant pool of the class file.

There are two types in the runtime constant pool: Symbolic References symbolic references and static constants.

Static constants require no subsequent parsing, and symbolic references require further parsing.

What are static constants and what are symbolic references? Let’s do an intuitive example.

String site="www.flydean.com"
Copy the code

The string “www.flydean.com” above can be thought of as a static constant because it does not change.

The name “site” of the string above is a symbolic reference that needs to be parsed at runtime. Why?

Because the value of site can be changed, we cannot determine its real value at the first time, which needs to be resolved in dynamic operation.

Static constant details

Static constants in the runtime constant pool are built from constant_pool in the class file. It can be divided into two parts: String constants and numeric constants.

String constants

A String constant is a reference to a String, built from the class CONSTANT_String_info structure:

CONSTANT_String_info {
    u1 tag;
    u2 string_index;
}
Copy the code

Tag is the tag of the structure, and string_index is the index of the string in the class constant pool.

The class constant pool for string_index is a CONSTANT_Utf8_info structure.

CONSTANT_Utf8_info {
    u1 tag;
    u2 length;
    u1 bytes[length];
}
Copy the code

What is CONSTANT_Utf8_info? It is the UTF-8 encoding of the String object to be created.

We know that Unicode ranges from 0x0000 to 0x10FFFF.

The UTF-8 variant is how Unicode is encoded. How does that code?

As you can see from the figure above, different Unicode ranges use different encodings.

Note that if a character takes up more than one byte, the big-endian big-first arrangement is used in the class file.

If the character range is below FFFF, a combination of two 3-byte formats is used.

With the CONSTANT_String_info structure in the class file, let’s look at the rules for creating run-time String constants from CONSTANT_String_info:

  1. Rule 1: If string. intern was called before and the result returned is the same as the code stored in CONSTANT_String_info, they refer to an instance of the same String.

  2. Rule 2: If they are different, a new String instance is created and the runtime String constant points to the instance of that String. Finally, the String intern method is called on the String instance. The intern method is called to add the String instance to the String constant pool.

Digital constant

Digital constants are built from the class files CONSTANT_Integer_info, CONSTANT_Float_info, CONSTANT_Long_info, and CONSTANT_Double_info.

Symbol reference in detail

Symbolic references are also built from constant_pool in class.

Symbolic references to class and interface come from CONSTANT_Class_info.

References to fields in class and interface come from CONSTANT_Fieldref_info.

The method reference in class comes from CONSTANT_Methodref_info.

The reference to the method in interface comes from CONSTANT_InterfaceMethodref_info.

The reference to the method handle comes from CONSTANT_MethodHandle_info.

The reference to the method type comes from CONSTANT_MethodType_info.

Symbolic references to dynamically computed constants come from CONSTANT_MethodType_info.

The reference to the dynamically computed call site comes from CONSTANT_InvokeDynamic_info.

String Pool String constant Pool

When we talked about the runtime constant pool, we mentioned that String constants are references to strings. So where do these strings go?

String Pool String constant Pool.

Only one copy of this String Pool is maintained in each JVM. Is shared by all classes.

String pools were stored in the methods area prior to 1.6. It was placed in the Java Heap after 1.8.

Note that String Pool holds instances of strings, that is, strings enclosed in double quotes.

So the question is, right?

String name = new String("www.flydean.com");Copy the code

How many objects are created?

conclusion

The constant pool in the class file holds string constants, class and interface names, field names, and other constants referenced in the class. Each class has a copy.

The runtime constant pool holds static constant and symbolic references built from the class file constant pool. Each class has a copy.

The string constant pool holds instances of “characters” that are referenced by the runtime constant pool.

Run-time constant pools correspond one-to-one to class or interface. If a class generates two instance objects, do they share the same run-time constant pool or do they generate two separate constant pools? Welcome to leave comments and discuss.

Link to this article: www.flydean.com/jvm-run-tim…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!