Talking about java compilation mechanism and running mechanism, talking about java Compilation
Composition of source files and bytecode
Source File: the extension name is followed by the java file, that is, the java source file.
Java source code compilation consists of the following three processes:
1. Analysis and input to the symbol table
2. annotation Processing
3. Semantic Analysis and generation of class files
The flowchart is as follows:
Bytecode: extends the file name followed by the class, that is, the java bytecode file.
The generated class file consists of the following parts:
-
Structure information. The class file consists of the class file format, version number, and the quantity and size of each part.
-
Metadata. Description and constant information in java source code, including parent class, implementation interface, declaration, scope and constant pool
-
Method information. Information corresponding to statements and expressions in java source code, including bytecode, exception processor table, evaluate stack, evaluate stack type record, local variable area size, and debug symbol information
Java compilation mechanism and running mechanism
Java compilation and running mechanism flowchart:
Step 1 (compile): First create the source file and then compile it from the java compiler in JVM. class file. when java compiles a class, if the class that this class depends on has not been compiled, the compiler automatically compiles the dependent class first and then references it. If the java compiler cannot find the. class file or. java source file of the class that the class depends on in the specified directory, the "Cant found sysbol" Exception error is reported.
Step 2 (run): java class execution is divided into two steps: (1) class loading (2) Class execution. Note that the JVM will load classes immediately only when the class is actively used during the first running of the program. In other words, the JVM does not load all the classes used into the memory at runtime, but is used. It is loaded only once when it has to be loaded!
Based on the above procedures, detailed procedures for running the program are described as follows:
(1) Find the compiled java program in the class path to get Test. after the class bytecode file is run, run java Test on the command line to start a JVM process.
Find a binary file named Test. class in the path, and load the class information in the Test. class file to the Method Area of the runtime data zone. This process is called loading the class. (Only the class information is in the method area.
To create an object, use the member variables in the class)
(2) The JVM finds the main function entry of the main method and holds a pointer to the current class (Test) constant pool, the first item in the constant pool is a symbolic reference to the Animal object, and
The first command in the main method is Animal animal = new Animal ("super_yc"), that is, let the JVM create an Animal object, but there is no Animal class information in the method area, so
The JVM will immediately load the Animal class and put the Animal class information into the method area. Therefore, the JVM replaces the first symbol reference in the constant pool with a pointer pointing directly to the Animal class in the method area.
(3) After loading the Animal class information, the JVM Virtual Machine allocates memory for an Animal class instance in the heap memory, and then calls its constructor to initialize the Animal class instance.
Reference of the type information of the Animal class in the method area (including the underlying implementation of dynamic binding in java. (When animal points to an Animal object, the reference is automatically placed in the stack, and The String constant
"Super_yc" is automatically placed in the constant pool of the method area, and the object is automatically placed in the heap Area)
(4) When animal. pringName () is used, JVM finds the animal object based on the Animal reference in the stack, and then locates the Animal class type in the method area based on the reference held by the Animal object.
Information method table, get the bytecode address of the pringName () function, and then start to run the function.