Reprinted please indicate the source: http://blog.csdn.net/xuefeng0707/article/details/9132339
The whole process of class loading is divided into five stages: Loading, verification, preparation, parsing, and initialization.
1. Load
This stage can be divided into three sections:
(1) Loading binary byte streams
Obtains the binary byte stream of this Class Based on the fully qualified class name (package name + class name.
The virtual machine specification does not specify where the binary byte stream is read. It can be a class file, a jar file, or a dynamic agent at runtime, as long as it is a compliant byte stream, the Class Loader determines the source of the byte stream.
(2) generate the data structure of the Method Area
Create the runtime data structure in the Method Area Based on the bytes read in the previous step.
(3) create a class instance
Create a java. Lang. class instance in the Java heap as an external interface for program code to access type data.
2. Verify
This phase verifies whether the Read Binary byte stream conforms to the storage format of class files in the specifications of the virtual machine. If not, a java. Lang. verifyerror exception or a subclass exception is thrown.
This stage is divided into four sections:
(1) File Format Verification
Verify that the byte stream complies with the class file format specification.
There are the following verification points:
- Indicates whether the magic number starts with 0xcafebabe.
- Whether the Primary and Secondary versions can be processed by the current virtual machine.
- Whether there are unsupported types in constants in the constant pool.
- Whether the items in the constant pool execute non-existent constants or non-conforming constants.
- ......
Note: The constant pool here is not a constant pool in the VM memory during runtime, but a part of the class file.(Http://www.blogjava.net/DLevin/archive/2011/09/05/358033.html) after file format verification, byte stream into the virtual machine memory method area for storage, the specific data structure is determined by the virtual machine. The subsequent verification is based on the data structure in the method area here. (2) Metadata verification performs semantic analysis to ensure that the class information described in the class file complies with the Java language specifications. That is, compilation can pass. There are the following verification points:
- Whether there is a parent class (except java. Lang. Object, all classes should have a parent class ).
- Whether the final class is inherited.
- If it is not an abstract class, whether all the methods to be implemented are implemented.
- ......
(3) perform bytecode verification to analyze the data flow and control flow, and verify the method bodies of the class to ensure that the class methods do not harm the security of virtual machines during operation.
- Make sure that the jump command does not jump to a command other than the method body.
- Make sure that the type conversion is valid.
- ......
(4) symbol reference verification in the "resolution" phase, when converting the symbol reference in the constant pool into drinking directly, you must first verify the symbol reference.
- Whether the corresponding class can be found by fully qualified names
- Can I find the specified field or method in the specified class?
- Is the accessibility of classes, fields, or methods appropriate.
- ......
If the verification fails, a subclass of Java. Lang. incompatibleclasschangeerror is thrown.
3. In the method area, allocate memory for the static field and set its initial value. The initial values set here are the default values defined by the Java Virtual Machine for various types. For example:
private static int value=123;
After the preparation phase, the value is 0 instead of 123. The value assignment is completed in the initialization phase <clinit>.
However, for static final fields, values are directly assigned at this stage. For example:
private static final int value=123;
After the preparation stage, the value is 123.
Default values for various types of Java virtual machines.
Data Type |
Default Value |
Int |
0 |
Long |
0l |
Short |
(Short) 0 |
Char |
'\ U000000' |
Byte |
(Byte) 0 |
Boolean |
False |
Float |
0.0f |
Double |
0.0d |
Reference |
Null |
4. Resolution
Put the class fileConstant poolSome symbolic references are converted into direct references of the runtime constant pool.
- Resolution class or interface
- Resolution Interface Method
5. Initialization
Run <clinit> ().
- <Clinit> () contains the assignment of all class variables in a class and the statement of static blocks (written in static. The execution sequence appears in the source file.
- <Clinit> () does not need to call <clinit> () of the parent class. This is done by virtual machines.
- The <clinit> () of the interface only contains the value assignment operation of fields in the interface. The <clinit> () of the parent interface does not need to be in the <clinit> () previously executed, the <clinit> () of the parent interface is executed only when the fields defined in the parent interface are actually referenced ().
- <Clinit> () is thread-safe. If multiple threads initialize a class at the same time, only one thread will execute initialization, and other threads will be blocked.