I. Overview
The JVM loads the data of the description class from the class file into memory, verifies the data, parses and initializes it, and eventually forms the Java type that can be used directly by the virtual machine, which is the class loading mechanism of the JVM.
Second, class loading process
Load-(Verify-Prepare-parse) (connection linking)-Initialize-use-Uninstall
1, "Load" is the first stage of "class loading", the JVM needs to complete the following 3 things: (1) A class of the fully qualified name to obtain the definition of such a binary byte stream, (2) The static storage structure represented by this byte stream into the runtime data structure of the method area ; (3) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.
2, verification is the first step in the connection phase, the purpose: to ensure that the class file's byte stream contains information that meets the requirements of the current virtual machine, and does not compromise the security of the virtual machine itself.
Approximately 4 stages of validation actions are performed: file format validation, metadata validation, bytecode validation, and symbolic reference validation.
3, ready to formally assign memory closed for class variables and set the initialization of class variables is worthwhile stage.
4. Resolve the JVM to replace the symbolic reference within the constant pool with the process of direct referencing.
5. Initialize the Java program code (or bytecode) that really starts to execute the definition in the class
Class Loader
The uniqueness of a class in the JVM is established by the ClassLoader that loads it and the class itself. In other words, it makes sense to compare two classes for equality, only if the two classes are loaded by the same classloader.
From the Java Developer's point of view, the class loader is divided into 3 types:
(1) Start class loader
(2) Extension class loader
(3) Application class Loader
Parent delegation Model for ClassLoader: see model. Except for the top-level initiators, the rest of the ClassLoader should be the parent class loader of the postage machine. Procedure: If a class loader receives a request for a class load, it first does not attempt to load the class itself, but instead delegates the request to the parent class loader, which is the case for each level of the loader, so all class load requests should eventually be routed to the top-level startup class loader. The child loader tries to load itself only when the parent loader has feedback that it cannot complete the load request (it does not find the desired class in its search scope).
The benefit of using this model: the Java class has a priority hierarchy with its loader, which is important for ensuring the stable operation of Java programs, and the code is concentrated in the Java.lang.ClassLoader LoadClass () method.
JVM class loading mechanism