The basic Java class loading process is described in detail

  • 2020-05-24 05:35:35
  • OfStack

The basic Java class loading process is described in detail

Basic process:

Loads the binary byte stream of the defined class based on the fully qualified name of the class. Converts the static storage structure represented by the byte stream into the runtime data structure of the method section An java.lang.Class object representing the class is generated in memory as a method to access the various data entries of the class

The array classes themselves are not created by the class loader, but by the java virtual machine. The element types of the array classes are loaded by the class loader.
Element type of the array class: the type of the array after removing all dimensions,

File format verification:

0xCAFEBABY magic number; The primary and secondary version number can be processed by the current virtual machine; Constant type; Index execution type; utf8 encodes the data type,

Metadata validation: bytecode description information semantic analysis:

Whether there is a parent class; Whether the parent class inherits the final modified class; Whether the non-abstract class implements the methods that need to be implemented in the parent class or interface; Class field, method coverage, overload contradiction;

Bytecode validation: through semantic flow and control flow analysis to determine the legitimacy, correctness of the program, method body analysis verification.

Symbol reference validation: when the virtual machine converts a symbol reference into a direct reference, the matching validation of information outside the class itself is carried out in the parsing phase Whether the corresponding class can be found by the fully qualified name described by characters in the symbolic reference; Specifies whether there is a descriptor in the class that matches the method field and the method and field described by the simple name; Accessibility of classes, fields, and methods in symbolic references.

Preparation: allocates memory for class variables in the method area and sets their initial values.

The initial value is usually the zero value of the data type, and the value modified by final is initialized directly to the corresponding value. Class variables are variables modified by static, distinguished from instance variables.

Parsing: the virtual machine replaces the symbolic reference in the constant pool with a direct reference procedure

CONSTANT_Class_info,CONSTANT_Fieldref_info,CONSTANT_Methodref_info..

Symbolic reference: a set of symbols to describe the referenced target, any form of literal, as long as the use can be unambiguously positioned to the target, is independent of the virtual machine memory implementation, regardless of whether the reference target is loaded. Direct reference: a pointer to the target directly, an offset or a handle indirectly positioned to the target, related to the memory of the virtual machine implementation, and the target object associated with the direct reference must be loaded. .

Initialization: starts executing the Java program code in the class definition. Execute the class constructor < cinit > () method,

< cinit > () :

The compiler automatically collects the assignment actions of class variables in a class based on the order in which they are defined in the class file and merges them with the static statement blocks, which can only access the variables previously defined. Unlike the class constructor, which does not need to be displayed to call the parent constructor, the virtual machine guarantees the subclass's < cinit > () of the parent class before execution < cinit > () has been executed. The static statement block in the parent class is executed first. < cinit > () is not required for a class or interface, and is not generated if there are no variable assignment operations or static statement blocks. Of the interface < cinit > () does not need to execute the parent interface first < cinit > (), the implementation class of the same interface < cinit > () no. Thread safety: virtual machines guarantee multithreaded environments < cinit > () properly locked, synchronized, only one thread can access the initialization class at a time < cinit > ()

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: