English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
JAVA source code compilation consists of three processes:
1Source code compilation mechanism.
2Class loading mechanism
3Class execution mechanism
Here, we mainly introduce the two mechanisms of compilation and class loading.
First, source code compilation
Code compilation is completed by the JAVA source code compiler. It mainly converts the source code into bytecode files (class files). The bytecode file format is mainly divided into two parts: constant pool and method bytecode.
Second, class loading
The lifecycle of a class starts from being loaded into the virtual machine memory and ends with being unloaded from memory. The process consists of seven stages, and all stages before initialization belong to the part of class loading.
Loading----Verification----Preparation----Parsing-----Initialization----Usage-----Unloading
The system may load a class when it is used for the first time, or it may use a preloading mechanism to load a class. When a Java program is executed, a Java virtual machine process is started. The two Java programs running are in two different JVM processes, and there is no data sharing between the two JVMs.
1Stage of loading
The loading process in this flow is a stage in the class loading mechanism. These two concepts should not be confused. The tasks that need to be completed at this stage include:
1The binary byte stream defining this class is obtained through the fully qualified name of a class.
2The static storage structure represented by this byte stream is converted into the runtime data structure of the method area.
3A Class object representing this class is generated in the Java heap, serving as the entry point to access the data in the method area.
Due to the lack of specification on where and how to obtain the binary byte stream of the class in the first point, this area leaves a lot of room for developers to play around. I will introduce this in the class loader later.
2The preparation phase
This stage officially allocates memory for class variables (variables modified by static) and sets the initial value of class variables, and this memory allocation occurs in the method area.
1Note that there is no memory allocation for instance variables here; instance variables will be allocated along with the object in the JAVA heap when the object is instantiated.
2The initial value set here usually refers to the zero value of the data type.
private static int a = 3;
The value of this class variable a after the preparation phase is 0, and the3The assignment to variable a occurs during the initialization phase.
3The initialization phase
Initialization is the final step of the class loading mechanism, at this time, the JAVA program code defined in the class is truly started to be executed. In the previous preparation phase, the class variables have been assigned the system's required initial value once, and the most important thing in the initialization phase is to initialize the class variables, with the focus on the order of initialization of various resources between parent and child classes.
There are two ways to specify the initial value of a class variable in Java classes:1By specifying the initial value when declaring a class variable;2By using a static initialization block to specify the initial value of a class variable.
The time of initialization
1When creating an instance of a class, there are the following options:1By using the new keyword to create an instance;2By creating an instance through reflection;3By creating an instance through deserialization.
new Test(); Class.forName("com.mengdd.Test");
2Call a class method (static method) of a class.
Test.doSomething();
3Access a class or interface variable, or assign a value to the class variable.
int b=Test.a; Test.a=b;
4Initialize a subclass of a class. When initializing a subclass, all the parent classes of the subclass will be initialized.
5Run a main class using the java.exe command directly.
In addition to the above methods that automatically initialize a class, other ways to access a class will not trigger the initialization of the class, known as passive reference.
1By referencing the static variable of the superclass, the initialization of the subclass will not be triggered.
public class SupClass { public static int a = 123; static { System.out.println("supclass init"); } } public class SubClass extends SupClass { static { System.out.println("subclass init"); } } public class Test { public static void main(String[] args) { System.out.println(SubClass.a); } }
Execution Result:
supclass init
123
2By defining a reference class through an array, the initialization of this class will not be triggered.
public class SupClass { public static int a = 123; static { System.out.println("supclass init"); } } public class Test { public static void main(String[] args) { SupClass[] spc = new SupClass[10]; } }
Execution Result:
3Referencing a constant will not trigger the initialization of the class
public class ConstClass { public static final String A= "MIGU"; static { System.out.println("ConstCLass init"); } } public class TestMain { public static void main(String[] args) { System.out.println(ConstClass.A); } }
Execution Result:
MIGU
When a class variable is modified with the final keyword, its value is already determined at compile time and placed in the constant pool, so when accessing the class variable, it is equivalent to directly obtaining it from the constant pool without initializing the class.
Initialization Steps
1If the class has not been loaded and linked yet, the program will first load and link the class.
2If the direct parent class of the class has not been loaded, the direct parent class will be initialized first.
3If there are initialization statements in the class, the system will execute these initialization statements in turn.
In the second step, if the direct parent class has another direct parent class, the system will repeat these three steps again to initialize this parent class, and so on. JVM always initializes the java.lang.Object class first.
The above is the introduction of the JAVA class loading mechanism (recommended) by the editor, hoping it will be helpful to everyone!
Declaration: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, the website does not own the copyright, does not undergo artificial editing, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w.3If you find any infringing content, please report it by email to: notice#w. When reporting, please replace # with @ and provide relevant evidence. Once verified, the website will immediately delete the infringing content.