Home >Java >javaTutorial >Detailed introduction to Java class loading process

Detailed introduction to Java class loading process

不言
不言forward
2019-03-07 16:49:523249browse

This article brings you a detailed introduction to the Java class loading process. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The process of Java files from encoding to final execution:

  1. Compile: Javac Compile the Java file into a .class file
  2. Run: Submit the .class file Run the

class loading process for the JVM

The JVM virtual machine loads the class information in the .class file into the memory and parses it to generate the corresponding class object. The JVM does not load all classes into memory from the beginning, but only loads them when it encounters a class that needs to be run for the first time, and only once.

Detailed introduction to Java class loading process

Loading

Load class bytecode files from various sources into memory through the class loader
JVM Three things to accomplish

  1. Get the binary byte stream that defines this class through the fully qualified name of a class.
  2. Static storage structure-> Runtime data structure in the method area
  3. Generate the java.lang.Class object of the corresponding class in the Java heap as the access entrance to these data in the method area.

Class loader

  • Start class loader
  • Extend class loader
  • Apply class loader Device
  • Custom class loader

Connection

The process of merging the binary code of the java class into the running state of the JVM

Verification

Ensure that the loaded byte stream complies with the virtual machine specifications and will not cause security errors

Verification classification

  • Verification of the file format, such as whether the constant Are there unsupported constants? Is there any non-standard or additional information in the file?
  • Verification of metadata, for example, does the class inherit a class modified by final? Do the fields and methods in the class conflict with the parent class? Is there an unreasonable overload?
  • Verification of bytecode ensures the rationality of program semantics, such as ensuring the rationality of type conversion.
  • Verification of symbol references, such as verifying whether the corresponding class can be found through the fully qualified name in the symbol reference? Verify the accessibility (private, public, etc.) in the symbol reference is accessible by the current class?

Preparation

Allocate memory for class variables (note, not instance variables) and assign initial values ​​(the default initial values ​​of the Java virtual machine according to different variable types)

Default initial value

  • The initial value of 8 basic types, the default is 0
  • The initial value of the reference type is null
  • The initial value of the constant is For the value set in the code
  • final static tmp = 456

Resolution

The process of replacing symbol references in the constant pool with direct references

Symbol reference: a string, but this string gives some relevant information that can uniquely identify a method, a variable, or a class

Direct reference: can be understood as a memory address, or an offset Quantity

For example, now call the method hello(), the address of this method is 1234567, then hello is a symbolic reference, 1234567 is a direct reference

In the parsing phase, the virtual machine will The symbolic references of class names, method names, and field names are replaced with specific memory addresses or offsets, that is, direct references

Initialization

to class variables (variables or statements modified by static ) Initialization is the process of executing the class constructor

Initialization process

  • When initializing a class, its parent class has not been initialized yet, its parent class will be initialized first
    - At the same time Contains multiple static variables and static code blocks, they are executed in top-down order

The above is the detailed content of Detailed introduction to Java class loading process. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete