Java code compilation is done by the Java source compiler, and the flowchart looks like this:
The execution of Java bytecode is done by the JVM execution engine, and the flowchart looks like this:
The entire process of compiling and executing Java code includes the following three important mechanisms: Java source compilation mechanism class loading mechanism class execution mechanism
Java source Code compilation mechanism
Java source code compilation consists of the following three processes: parsing and input to symbol table annotation processing semantic analysis and generating class files
The flowchart looks like this:
The last generated class file consists of the following parts: structure information. Includes the class file format version number and the number and size of each part of the information meta data. Corresponds to the information declared and constants in the Java source code. Contains declaration information, domain and method declaration information, and constant pool method information for the class/inherited superclass/implementation interface. Corresponds to the information in the Java source code for statements and expressions. Contains bytecode, exception handler table, evaluation stack and local variable area size, evaluation stack type record, debug symbol information
class loading mechanism
Class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy of classes and loading order can be described in the following illustration:
1) Bootstrap ClassLoader
Responsible for loading all class in Jre/lib/rt.jar in $java_home, implemented by C + +, not ClassLoader subclass
2) Extension ClassLoader
Some jar packages that are responsible for loading extensions in the JAVA platform, including the jar packages in Jre/lib/*.jar or-djava.ext.dirs specified directories in $java_home
3) App ClassLoader
Responsible for documenting the jar package and class in the directory specified in Classpath
4) Custom ClassLoader
ClassLoader that belong to the application to customize according to their own needs, such as Tomcat, JBoss will be implemented according to the Java EE specification ClassLoader
The load process checks to see if the class is loaded, the check order is bottom-up, from custom ClassLoader to Bootstrap ClassLoader, and as long as a classloader is loaded, the class is considered loaded. Ensure that only all classloader of this class are loaded once. The order of loading is from top to bottom, which is to try to load this class from the top level.
class execution mechanism
The JVM is based on the stack architecture to execute class byte code. After the thread is created, both the program counter (PC) and stack (stack) are generated. The program counter holds the offset of the next instruction to be executed in the method, the stack holds each stack frame, each stack frame corresponds to each method of each call, and the stack frame has the local variable area and the operation number stack two parts, The local variable area is used to store the local variables and parameters in the method, and the operand stack is used to store the intermediate results produced during the execution of the method. The structure of the stack is shown in the following illustration: