The process of class loading in the JVM

1. What is class loading

The process of loading the .class file into the memory and obtaining the class object.

Second, the class loading process

1. load

Find the .class file and read the file content

2. Verification

Verify that the found file is a .class file, and the .class file has a clear data format

3. Prepare

Allocate space for class objects.

Note that this space is an uninitialized space, and the data in the memory space is all 0.

4. Analysis

Symbolic references in the string constant pool are replaced with direct references. (initialized for string constants)

The symbolic references in the string constant pool are string constants, which already exist in the .class file. These string constants don't know their actual addresses in memory, only their relative positions to each other.
After initialization, each string constant has its actual address in memory (direct reference), but its relative position to each other has not changed. Symbolic references are replaced by direct references.

To give an example in life, suppose the class organizes to go to the movies together, the teacher will arrange the students to line up, and Xiao Yu and Xiao Gu are naturally lined up together (symbolic reference). Before entering the theater, Xiao Yu and Xiao Gu remained in the same row. After arriving at the cinema, when they were divided into seat numbers (direct quotes), they were naturally next to each other. (relative position unchanged).

5. Initialization

Initialize class objects

Initialize static members, execute static code blocks, and if there is a parent class, you need to load the parent class.

3. When to classload

Class loading is not loaded when it is not necessary.
It will only be loaded under the following three conditions.
1. An instance of the class is created;
2. The static method of the class is used;
3. The use of a subclass triggers the loading of the parent class.

4. Parental delegation model

Actually 'single parent'.

1. Three types of loaders

  • BootStrap ClassLoader loads classes in the java standard library
  • Extension ClassLoader loads the thunder in the java non-standard library (but it is the class of the Sun/Oracle extended library)
  • Application ClassLoader loads classes created by itself

2. Loading process

insert image description here

total

I wish you all the best, good health and happiness every day, see you next time! ! !

Guess you like

Origin blog.csdn.net/m0_71690645/article/details/131705353