Java code compilation is completed by the Java source code compiler. The flowchart is as follows:
The execution of Java bytecode is completed by the JVM execution engine. The flowchart is as follows:
The entire Java code compilation and execution process includes the following three important mechanisms:
- Java source code compilation Mechanism
- Class Loading Mechanism
- Class execution mechanism
Java source code compilation Mechanism
Java
Source code compilation consists of the following three processes:
- Analysis and input to symbol table
- Annotation Processing
- Semantic Analysis and generation of class files
The flowchart is as follows:
The generated class file consists of the following parts:
- Structure information. Including the version number of the class file format and the quantity and size of each part
- Metadata. Corresponds to the declaration and constant information in Java source code. Contains the declaration information, domain and method declaration information, and constant pool of classes/inherited superclasses/Implemented interfaces
- Method information. Corresponds to the statements and expressions in the Java source code. Including bytecode, exception processor table, evaluation stack and local variable area size, evaluation stack type record, debugging Symbol Information
Class Loading Mechanism
JVM class loading is completed by classloader and its sub-classes. The class hierarchy and loading sequence can be described from the following:
1) bootstrap classloader
Loads all the classes in JRE/lib/RT. jar in $ java_home, implemented by C ++, not the classloader subclass.
2) Extension classloader
Loads some jar packages for the extended functions of the Java platform, including the jar packages under the specified directory of JRE/lib/*. jar or-djava. Ext. dirs in $ java_home.
3) app classloader
Records the specified jar package in classpath and the class in the directory.
4) custom classloader
Classloader defined by applications according to their own needs. For example, Tomcat and JBoss implement classloader based on J2EE specifications.
During the loading process, the system first checks whether the class has been loaded. The check sequence is from bottom to top. From custom classloader to bootstrap classloader, this class is regarded as loaded as long as a classloader has been loaded, make sure that this class is only loaded once by all classloader. The order of loading is from top to bottom, that is, the upper layer tries to load this type.
Class execution mechanism
JVM executes class bytecode Based on the stack architecture. After a thread is created, a program counter (PC) and a stack are generated. The program counter stores the offset of the next command to be executed in the method, and stack frames are stored in the stack, each stack frame corresponds to each call of each method, and the stack frame is composed of a local variable area and an operand stack. The local variable area is used to store local variables and parameters in the method, the operand stack is used to store intermediate results generated during method execution. Shows the stack structure: