First, let's look at the standard structure defined by the JVM specification:
First, let's look at how class files are compiled and generated. It can be seen that javac Compilation
Code There are three steps. Next we will analyze the three steps: 1. The process of analyzing and inputting the parse into the symbol table (parse and enter) Parse is lexical and syntax analysis. The lexical analysis completes the conversion of the code string into the token sequence. The syntax analysis completes the generation of the abstract syntax tree from the token Sequence Based on the syntax. Enter the symbol to the symbol table. Generally, this includes determining the super type and interface of the class. Add the default constructor as needed to input the symbols in the class to its own symbol table. 2. The annotation processing step is used to process custom annotation. 3. Semantic Analysis and generation of class file (analyse and generate) The analyse step performs a series of Semantic Analysis Based on the abstract syntax tree, including the elements and variables and methods in the syntax tree, such as names and expressions, type and so on. Convert the generic type to Java. eliminate useless code (if (false ){...}); inspection and so on. Generate the class file through (COM. Sun. Tools. javac. JVM. Gen. The class file contains the structure information (including the version number of the class file format and the quantity and size of each part), metadata (corresponding to the declaration and constant information), and method information. Class loading mechanism: Loading, linking, and initialization. Then let's look at the class loading mechanism. Load loads binary bytecode into JVM. Link is responsible for verifying the format of binary bytecode, initializing static variables in the loading class, and calling interfaces and classes in the parsing class. The static initialization code, constructor code, and static property code in the initialize (initialization) Execution class will be executed in violation of the following 4 conditions: 1. new2 is called, reflection calls method 3 in the class, subclass calls initialization 4, and the initialization class specified during JVM startup. About how to use the class loading mechanism
Article . Class execution mechanism: During the source code compilation stage, the source code is compiled into the JVM bytecode. The JVM bytecode is an intermediate code method, which must be interpreted and executed by the JVM at runtime, this method is called the bytecode interpretation execution method. Compilation and execution: the efficiency of interpretation is relatively low. JDK supports compiling bytecode into machine code. Compile In the runtime city, usually called JIT compiler. Compilation of code with high execution frequency, and interpretation of code with low execution frequency, is also called hotspot VM. Reflection execution: reflect, you know. It should be noted that the getmethod method has excellent performance. On the one hand, it is because of permission verification, on the other hand, scanning all methods and copying method objects, therefore, the method object returned by getmethod should be cached in the system that uses multiple reflection calls. The performance of method. Invoke is only a little lower than that of direct call.