Javac implementation process

It mainly introduces the implementation process and principle of Javac.

First figure out what is Javac?

Javac is a compiler that converts one language into another language specification. The role of the compiler is to convert the source code that conforms to the Java language specification into a bytecode file that can be recognized by the JVM virtual machine.

For java. The task of javac is to convert the java source code into binary code that the jvm can recognize. Convert .java files to .class files, these .class files are also bytecode files that only the JVM can recognize.

 

 

 

The principle of the compilation process:

1. Lexical analysis: first read the source code in bytes, then find out the defined grammatical keywords (if/else/for, etc.), and then judge which keywords are in line with the java language specification, and return after sorting and analysis Some normalized token streams

2. Syntax analysis: analyze the Token flow. It is analyzed whether the combination of these keywords conforms to the Java language specification. After analysis, an abstract syntax tree that conforms to the Java specification is generated. An abstract syntax tree is a structured syntax expression, and its function is to combine lexical expressions in a structured form.

3. Semantic analysis: further analyze the generated abstract structure tree, and convert the complex grammatical structure into a simple, easy-to-understand and read grammatical structure. For example: convert the enhanced for loop foreach into a for loop structure. After semantic analysis, a more specific abstract structure tree will be generated.

4. Bytecode: After the above three steps, the java source code can be converted into other bytecode files for the Java Virtual Machine (JVM).

The entire business process:

 

 The basic principles of javac are introduced here. If you are interested, you can study the source code of Javac, which can be downloaded from OpenJdk.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325470494&siteId=291194637