Java Virtual Machine dynamically loads, links and initializes classes and interfaces. So, how does a Class binary file be loaded into memory by the JVM? How does the JVM describe a Java class? How can a class or interface get the JVM to execute? How are classes or interfaces initialized?
With these questions, this article will discuss:
- JVM load
- JVM Links
- JVM initialization
A Class file is read by the JVM and then loaded, linked, and initialized into a format that can be recognized by the JVM, and ultimately in memory is a Java.lang.Class object. In the virtual machine specification, these phases are not specified in order, and some phases of the HotSpot VM are cross-cutting.
1. JVM Loading
Loading is to parse out the class or interface from the byte stream, in the format of the class file, and create the appropriate class or interface. When the JVM starts, the following kinds of loaders are used:
Launches the ClassLoader, which is responsible for loading the Java Core Class library (such as Rt.jar) located in the <java_home>\jre\lib directory, which is written using local methods, not a Java class. The class that is loaded by it, when called getClassLoader (), returns NULL.
The extension ClassLoader, which is responsible for loading class files located in the <java_home>\jre\lib\ext directory, is implemented by Sun.misc.launcher$extclassloader.
The System class loader, also called the Application ClassLoader (Application class loader), is responsible for loading the class library located on the CLASSPATH environment variable path. The default loader for the program is it, and of course you can customize the loader. It is implemented by Sun.misc.launcher$appclassloader.
- user-defined Class Loader
User-defined class loader.
The loading process is: Verify the magic number and master version number, read the static constant pool definition in the class file, create a run constant pool and return a handle, read the access control, read the This class and its fully qualified name (in the constant pool), super class and fully qualified name, read the interface information, Includes local and parent interfaces, reads field information, allocates storage, reads method information, creates Instanceklass (objects that describe Java classes inside the JVM), assigns values based on the information read above, creates a mirror class Java.lang.Class, Initializes a static domain, and notifies the class that it is loaded.
At run time, the fully qualified name of a class or interface is not uniquely identified, but is identified by the name and the ClassLoader that loaded it. So when using Instanceof,equals () to determine if two classes are the same, the requirement is that two classes be loaded by the same classloader. There is a system dictionary inside the JVM that records information such as all classes loaded by the system, class loaders, and other modules to provide retrieval services.
To avoid repeated loading and to ensure the security of the Java type system, the JVM, when loaded, uses the parental delegation model (parents delegation model), which represents a hierarchical relationship of the ClassLoader and is implemented using a combination.
When a class loader loads a class, first it does not first attempt to load the class itself, but rather to delegate the load request to the parent class loader, each layer is the same, the request is uploaded to the top level of the Launcher class loader, only when the parent class loader feedback itself can not complete the load, the child classloader will load itself.
The JVM creates a Instanceklass object for each loaded Java class to represent the Java class. It contains all the information required to run a Java class, and its member variables are initialized during the parsing phase.
2. JVM Links
Class files are established by symbolic references (symbolic references exist as strings), the JVM dynamically loads classes or interfaces, links them with dynamic links, and implements method invocations and mutual references. Links can be divided into validation, preparation and parsing.
Used to ensure that the class file conforms to the requirements of the JVM, verifies the correctness of the bytecode, and validates the metadata information of the class.
Allocates memory for class variables and assigns default values for each type, and if it is a final static decorated field, it is directly initialized to the specified value.
Converts a symbolic reference in a constant pool to a direct reference, that is, the actual memory address of the runtime, which consists of class, interface, field, and Method 4-class symbolic references.
3. JVM Initialization
Initialization phase, is the implementation of the Java initialization method, there are two methods: one is the class initialization method <clinit> (static block, statement initialization), one is the instance initialization method <init> (constructor), both methods are automatically generated by the compiler, Called implicitly by the JVM itself during the initialization phase.
The JVM guarantees that the parent class executes the <clinit> method before the subclass, which means that the static block and variable initialization of the parent class precedes the execution of the subclass, and in a multithreaded environment, the JVM guarantees the thread safety of the <clinit> method.
jvm-load, link, initialize