Home > Java > JavaInterview questions > Interviewer: Tell me about the class loading process (10 diagrams)

Interviewer: Tell me about the class loading process (10 diagrams)

Release: 2023-08-23 15:05:47
forward
1630 people have browsed it

##Loading

When we want to use a class, we need to Load the class into memory through ClassLoader

"The class loading phase mainly completes the following three things"

  1. Get the class through the full class name The binary stream of the parsing class
  2. creates an instance of the java.lang.Class class for the data structure in the method area
  3. . Indicates this type, as the access entry of this class in the method area
Interviewer: Tell me about the class loading process (10 diagrams)

"The method of obtaining the binary stream of the class through the full class name is Many kinds"

  1. Get it from the zip package
  2. Get it from the network
  3. Runtime calculation generation, such as dynamic proxy technology
  4. ...

"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"

Link

"This stage of linking is mainly divided into 3 parts, verification, preparation, and analysis"

Verification

"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 stagesInterviewer: Tell me about the class loading process (10 diagrams)"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."

Preparation

"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?

  1. Class static variables are of basic data type, and are modified by final
  2. Class static variables are of String type, are modified by final, and are literal Assignment in the form of quantity

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");
}
Copy after login

Interviewer: Tell me about the class loading process (10 diagrams)"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" Interviewer: Tell me about the class loading process (10 diagrams)

Analysis

"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;
    }
}
Copy after login

Interviewer: Tell me about the class loading process (10 diagrams)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)

初始化

「执行类静态成员变量赋值语句和静态代码块中的语句」

Interviewer: Tell me about the class loading process (10 diagrams)

我们把上面的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;
    }
}
Copy after login

可以看到字节码中包含了3个方法,getName方法我们知道,方法里面执行了哪些逻辑?Interviewer: Tell me about the class loading process (10 diagrams)从字节码的角度分析一波

方法」

Interviewer: Tell me about the class loading process (10 diagrams)

从字节码可以看到方法的主要逻辑为

  1. 调用父类的方法
  2. 非静态成员变量赋值
  3. 执行构造代码块
  4. 执行构造函数

Interviewer: Tell me about the class loading process (10 diagrams)方法」Interviewer: Tell me about the class loading process (10 diagrams)从字节码可以看到方法的主要逻辑为

  1. Execute the assignment statement of the static variable
  2. Execute the statement in the static code block
  3. One thing to note Yes, "The Java virtual machine ensures that the parent class's method has been executed before the subclass's method is executed."

# #"It is still necessary to understand the role of the and methods, because some interview questions often ask about static code blocks, construction code blocks, and the execution order of constructors."

I will directly summarize the conclusion here. You can write a demo to verify it

"Execution sequence without inheritance"

  1. Static code block and static member variables, the execution order is determined by the writing order (it will only be executed once)
  2. Construct the code block and non-static member variables, the execution order is determined by the writing order
  3. Constructor

「Execution order with inheritance」

  1. Parent class Static (static code block, static member variables), subclass's static (static code block, static member variables) (will only be executed once)
  2. Non-static (constructor) of the parent class Code block, non-static member variables), parent class's constructor
  3. Non-static subclass (construction code block, non-static member variables), subclass's constructor

Uninstall

Garbage collection occurs not only in the heap, but also in the method area. However, the conditions for recycling type data in the method area are relatively strict.

Interviewer: Tell me about the class loading process (10 diagrams)The following figure is an example. I want to recycle the Simple class in the method areaInterviewer: Tell me about the class loading process (10 diagrams)

  1. Need to ensure that the Sample class and its subclasses in the heap have been recycled
  2. The MyClassLoader that loads the Sample class has been recycled
  3. The Class object corresponding to the Sample class has been recycledInterviewer: Tell me about the class loading process (10 diagrams)

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

Summary

Class loading processInterviewer: Tell me about the class loading process (10 diagrams)

Variable assignment processInterviewer: Tell me about the class loading process (10 diagrams)

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!

Related labels:
source:Java后端技术全栈
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template