"The class loading phase mainly completes the following three things"
"The method of obtaining the binary stream of the class through the full class name is Many kinds"
"For the loading phase of non-array types, That is, you can use the built-in class loader of the Java virtual machine to complete it, or you can use a user-defined class loader to complete it"
"This stage of linking is mainly divided into 3 parts, verification, preparation, and analysis"
"The verification phase is mainly to ensure that the Class file The format is correct and will not endanger the security of the virtual machine when running."
There are many rules in the verification phase, but they are roughly divided into the following four stages"For specific details, I will I won't explain it in detail. You can read "In-depth Understanding of Java Virtual Machine". This article is intended to make a summary and grasp the overall process of class loading without elaborating on the details."
"The preparation phase mainly allocates memory for the static variables of the class and initializes them to default Value"
The default values of common data types are as follows
Data type | Default value |
---|---|
byte | (byte)0 |
short | (short)0 |
int | 0 |
long | 0L |
float | 0.0f |
double | 0.0d |
boolean | false |
char | '\u0000' |
reference | null |
"If the ConstantValue attribute exists in the field attribute table of the class static variable, the assignment statement will be executed directly"
So under what circumstances does the field attribute table of the class static variable exist? What about the ConstantValue property?
In order to conveniently view the bytecode of the Class file, I downloaded a plug-in jclasslib Bytecode viewer in IDEA, which is very convenient. Use the following code to verify it in the form of bytecode
public class Person { private static int age = 10; private static final int length = 160; private static final String name = "name"; private static final String loc = new String("loc"); }
"So the length and name attributes will be assigned the values specified by ConstantValue during the preparation phase"
"So at which stage will the age and loc attributes be assigned? It is during the initialization stage, which will be introduced in detail later"
"Convert symbolic references (in the constant pool) of classes, interfaces, fields and methods into direct references" Symbolic reference: Use a set of symbols to describe the referenced target Direct reference; direct pointer to the target
Join me and write a class as follows
public class Student { private String name; private int age; public String getName() { return this.name; } }
Taking fields as an example, the objects corresponding to name and age do not directly point to the memory address , instead use strings to describe (i.e. symbolic references). The parsing stage is to convert these descriptions into pointers directly pointing to the target (that is, direct references)
「执行类静态成员变量赋值语句和静态代码块中的语句」
我们把上面的Student代码改成如下形式
public class Student { private String name; private int age = 10; private static int gender = 1; { System.out.println("构造代码块"); } static { System.out.println("静态代码块"); } public Student() { System.out.println("构造函数"); } public String getName() { return this.name; } }
可以看到字节码中包含了3个方法,getName方法我们知道,
「
从字节码可以看到
「
# #"It is still necessary to understand the role of the
"Execution sequence without inheritance"
「Execution order with inheritance」
The following figure is an example. I want to recycle the Simple class in the method area
It can be seen that the conditions for recycling type data in the method area are relatively strict, but the effect is minimal. Therefore, some garbage collectors will not recycle type data in the method area
Class loading process
Variable assignment process
The above is the detailed content of Interviewer: Tell me about the class loading process (10 diagrams). For more information, please follow other related articles on the PHP Chinese website!