Lifecycle of Java virtual machines and programs
1. Why does the lifecycle of a Java Virtual Machine end?
1. Execute the system. Exit () method.
2. The execution of the program ends normally.
3. An exception or error occurred while executing the program.
4. the Java Virtual Machine Process is terminated due to an error in the operating system.
2. class loading, connection, and initialization
Load: search for and load binary data of the class. When the class is loaded. the binary data in the class file is read into the memory, stored in the Method Area of the data area, and a Java is created in the heap area. lang. class Object, used to encapsulate the data structure of the class in the method area. Load. class file: directly loaded from the local system; downloaded over the network. class file; from zip. jar and other archive files. class file; extracted from a dedicated database. class file; dynamically compile the Java source file. class File
The final product of class loading is the class object located in the heap area. The Class Object encapsulates the data structure of the class in the Method Area and provides an interface for Java programmers to access the data structure.
Connection: Verify: ensure the correctness of the loaded class.
Preparation: allocate memory for static variables of the class and initialize it as the default value.
Resolution: converts a symbolic reference in a class to a direct reference.
Initialization: Assign the correct initial value to the static variables of the class.
3. java programs can use classes in two ways: active and passive. All Java Virtual Machine implementations must initialize each class or interface when it is actively used by Java programs for the first time. The active use of Java is mainly divided into six methods:
Create a class instance;
Access a static variable of a class or interface, or assign a value to the static variable;
Call static methods of the class;
Reflection (class. forname ("com. test "))
Initializes a subclass of a class.
Class marked as a startup class when a Java Virtual Machine is started
Except for the above 6 cases, all other cases are passive, and class initialization will not occur.
4. class loading
There are two types of class loaders
Java virtual machine comes with the following loaders: Root loader (bootstrap), extension, and system loader (system)
The user-defined class loader is a subclass of Java. Lang. classloader. You can customize the class loading method.
The JVM specification allows the class loader to load a class in advance when it is expected to be used. the class file is missing or has an error. The class loader must take the initiative to use this class for the first time before reporting the error (linkageerror). If this class has not been actively used by the program, the class loader will not report errors.
Class Singleton {
Public static int counter1;
Public static int counter2 = 0;
Private Static Singleton = new Singleton ();
Private Singleton (){
Counter1 ++;
Counter2 ++;
}
Public static Singleton getinstance (){
Return Singleton;
}
}
Class test {
}
Public class jvmtest1 {
Public static void main (string [] ARGs ){
Singleton = singleton. getinstance ();
System. Out. println (singleton. Class. getclassloader ());
System. Out. println ("counter1 =" + singleton. counter1 );
System. Out. println ("counter2 =" + singleton. counter2 );
}
}
In the main method, the static method of Singleton is called to allocate space for static variables and assign it to the default value. Singleton is null, counter1 is 0, counter2 is 0, and Initialization is performed, call the singleton constructor, resulting in counter1 = 1, counter2 = 1; then counter1 is not initialized or 1, counter2 is initialized to 0, so the final result is
[Email protected]
Counter1 = 1
Counter2 = 0
Class Singleton {
Public static int counter1;
Public static int counter2 = 0;
Private Static Singleton = new Singleton ();
Private Singleton (){
Counter1 ++;
Counter2 ++;
}
Public static Singleton getinstance (){
Return Singleton;
}
}
Class test {
}
Public class jvmtest1 {
Public static void main (string [] ARGs ){
Singleton = singleton. getinstance ();
System. Out. println (singleton. Class. getclassloader ());
System. Out. println ("counter1 =" + singleton. counter1 );
System. Out. println ("counter2 =" + singleton. counter2 );
}
}
Likewise, it can be analyzed.
[Email protected]
Counter1 = 1
Counter2 = 1
5. class initialization
Class initialization steps:
1. If this class is not loaded or connected, load and connect it first.
2. There is a direct parent class added to the class, and this parent class has not been initialized, then initialize the direct parent class first.
3. If there are initialization statements in the class, execute these initialization statements in sequence.
Note: 1. The final type compilation constant will not initialize the class at that time.
2. When a Java Virtual Machine initializes a class, it requires that all its parent classes have been initialized, but this rule does not apply to interfaces.
When initializing a class, it does not initialize the interface it implements first.
When initializing an interface, it does not initialize its parent interface first.
Therefore, a parent interface will not be initialized because of its sub-interface or implementation class initialization. Initialization of a specific interface is returned only when the program uses static variables of a specific interface for the first time.
3. Only when the static variables or static methods accessed by the program are indeed defined in the current class or the current interface can they be considered as active use of classes or interfaces.