


Interviewer: Tell me about the class loading process (10 diagrams)
Aug 23, 2023 pm 03:05 PM##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"
- Get the class through the full class name The binary stream of the parsing class
- creates an instance of the java.lang.Class class for the data structure in the method area
- . Indicates this type, as the access entry of this class in the method area

"The method of obtaining the binary stream of the class through the full class name is Many kinds"
- Get it from the zip package
- Get it from the network
- Runtime calculation generation, such as dynamic proxy technology
- ...
"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 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."
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?
Class static variables are of basic data type, and are modified by final 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"); }
"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"
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; } }
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)
初始化
「執(zhí)行類靜態(tài)成員變量賦值語(yǔ)句和靜態(tài)代碼塊中的語(yǔ)句」

我們把上面的Student代碼改成如下形式
public class Student { private String name; private int age = 10; private static int gender = 1; { System.out.println("構(gòu)造代碼塊"); } static { System.out.println("靜態(tài)代碼塊"); } public Student() { System.out.println("構(gòu)造函數(shù)"); } public String getName() { return this.name; } }
可以看到字節(jié)碼中包含了3個(gè)方法,getName方法我們知道,從字節(jié)碼的角度分析一波
「

從字節(jié)碼可以看到
調(diào)用父類的 方法 非靜態(tài)成員變量賦值 執(zhí)行構(gòu)造代碼塊 執(zhí)行構(gòu)造函數(shù)
「
從字節(jié)碼可以看到
Execute the assignment statement of the static variable Execute the statement in the static code block 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
"Execution sequence without inheritance"
- Static code block and static member variables, the execution order is determined by the writing order (it will only be executed once)
- Construct the code block and non-static member variables, the execution order is determined by the writing order
- Constructor
「Execution order with inheritance」
- Parent class Static (static code block, static member variables), subclass's static (static code block, static member variables) (will only be executed once)
- Non-static (constructor) of the parent class Code block, non-static member variables), parent class's constructor
- 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.The following figure is an example. I want to recycle the Simple class in the method area
Need to ensure that the Sample class and its subclasses in the heap have been recycled The MyClassLoader that loads the Sample class has been recycled The Class object corresponding to the Sample class has been recycled
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 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

You must know Spring, so let’s talk about the order of all notifications of Aop. How does Spring Boot or Spring Boot 2 affect the execution order of aop? Tell us about the pitfalls you encountered in AOP?

OOM means that there is a vulnerability in the program, which may be caused by the code or JVM parameter configuration. This article talks to readers about how to troubleshoot when a Java process triggers OOM.

?This article will take a look at 5 interview questions about the Java String class. I have personally experienced several of these five questions during the interview process. This article will help you understand why the answers to these questions are like this.

Don’t underestimate the written examination questions of many companies. There are pitfalls and you can fall into them accidentally. When you encounter this kind of written test question about cycles, I suggest you think calmly and take it step by step.

Last week, a friend in the group went for an interview with Ping An Insurance. The result was a bit regretful, which is quite a pity, but I hope you don’t get discouraged. As you said, basically all the questions encountered in the interview can be solved by memorizing the interview questions. It’s solved, so please work hard!

The extra chapter of the Java concurrent programming series, C A S (Compare and swap), is still in an easy-to-understand style with pictures and texts, allowing readers to have a crazy conversation with the interviewer.

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

The data structure of Java is the focus of the interview. Anyone who has participated in a Java interview must have some experience. When interviewers ask such questions, they often want to check whether you have studied the underlying structures of commonly used data types in Java, rather than simply staying at the level of "knowing how to use".
