Class loading mechanism in Java

1. What is class loading?

Read the binary data in the .class file of the class into memory, put it in the method area of ​​the runtime data area, and then create a java.lang.class object on the heap to encapsulate the data of the class in the method area structure.

Note that the final product of class loading is the class object.

2. The life cycle of the class: Loading. Validation. Prepare to parse and initialize, in fact, the loading of real classes is not necessarily carried out in this order, and the parsing phase may be delayed in execution, which is related to the loading mechanism of the Java language

Java supports dynamic binding technology

The work of the virtual machine during class loading:

Get a binary byte stream through a class's permission name

Convert the static storage structure represented by this byte stream into a runtime data structure in the method area'

Generate an object representing this class on the Java heap java.lang.class object as the access entry to the data in the method area

The purpose of verification is to ensure that the information contained in the byte stream in the Class file meets the requirements of the current virtual machine and will not endanger the security of the virtual machine itself. Different virtual machines may implement class verification differently, but they generally complete the following four stages of verification: file format verification, metadata verification, bytecode verification, and symbol reference verification.

1)文件格式的验证:验证字节流是否符合Class文件格式的规范,并且能被当前版本的虚拟机处理,该验证的主要目的是保证输入的字节流能正确地解析并存储于方法区之内。经过该阶段的验证后,字节流才会进入内存的方法区中进行存储,后面的三个验证都是基于方法区的存储结构进行的。 2)元数据验证:对类的元数据信息进行语义校验(其实就是对类中的各数据类型进行语法校验),保证不存在不符合Java语法规范的元数据信息。 3)字节码验证:该阶段验证的主要工作是进行数据流和控制流分析,对类的方法体进行校验分析,以保证被校验的类的方法在运行时不会做出危害虚拟机安全的行为。 4)符号引用验证:这是最后一个阶段的验证,它发生在虚拟机将符号引用转化为直接引用的时候(解析阶段中发
生该转化,后面会有讲解),主要是对类自身以外的信息(常量池中的各种符号引用)进行匹配性的校验

The preparation phase is the phase of formally allocating memory for class variables and setting the initial values ​​of class variables, all of which will be allocated in the method area.
The resolution phase is the process by which the virtual machine replaces symbolic references in the constant pool with direct references.

The class initialization phase is the last step of the class loading process. In the previous class loading process, except for the loading (Loading) phase, the user application can participate through the custom class loader, and the rest of the actions are completely dominated and controlled by the virtual machine. At the initialization stage, the Java program code defined in the class is actually executed.
Initialization, assigning correct initial values ​​to the static variables of the class, the JVM is responsible for initializing the class, mainly initializing the class variables. There are two ways to initialize class variables in Java:

①声明类变量时指定初始值

②使用静态代码块为类变量指定初始值

以上就是Java类的加载机制的总结

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324596476&siteId=291194637