Javac Compilation principle

Source: Internet
Author: User

Written in front JDK & JRE

The JRE (Java Runtime enviroment) is the Java Runtime environment. for users of Java programs, not developers . If you download and install only the JRE, your system can only run Java programs. The JRE is the set of environments necessary to run a Java program, including the JVM standard implementation and the Java Core Class library. It includes Java virtual machines, Java platform core classes, and supporting files. It does not contain development tools (compilers, debuggers, etc.).


The JDK (Java Development Kit), also known as J2SDK (JAVA2 Software Development Kit), is the Java SDK, which provides a Java development environment that provides tools such as compiler Javac Used to compile Java files into a class file) and the runtime environment (provides JVM and runtime helper packages for parsing class files to make it run). If you download and install the JDK, you can not only develop Java programs, but also have a platform to run Java programs. The JDK is the core of the entire Java, including the Java Runtime Environment (JRE), a stack of Java tools Tools.jar, and the Java Standard Class library (Rt.jar).

JDK also could is called as SDK (software development Kit) for Java.

What is 1.Javac?

Javac is a compiler that translates a language specification into another language specification . compilers are usually the ones that translate understandable language specifications into language specifications that are easily understood by machines .

Javac's task is to compile Java code into Java bytecode, which is the binary code that the JVM can recognize. On the surface, converting a. java file into a. class file actually translates Java's source code into a series of formatted binary digits, and only the JVM can correctly identify what it means.

2.Javac Workflows and analogies

  2.1 Find grammar keywords such as if else for while, etc., this process is called lexical analysis (The result of lexical analysis is to find some normalized token streams from the source code)

Analogy: the verb noun of distinguishing words and punctuation in human language

  2.2 Check that these keywords are grouped together to conform to the Java language Specification, a process called parsing (the result of parsing is an abstract syntax tree that conforms to the Java specification)

Analogy: Is there a human language in which the correct grammar is correct for both the host and the guest-predicate combination?

  2.3 Translating difficult and complex grammars into simpler grammars, a process called semantic analysis (for example, if you turn foreach into for, the result is an abstract syntax tree that comes out of the annotation , which is closer to the grammar rules of the target language)

analogy: translation of hard-to-understand classical Chinese into vernacular in human language

  2.4 generates bytecode through bytecode generators, generating bytecode from annotated abstract syntax trees, which translates a data structure into another data structure

Analogy: After translating Chinese words into English words, they are assembled into English sentences according to English grammar.

To sum up, Javac mainly has four modules, namely lexical analyzer, parser, semantic parser and code generator.

3. Lexical analyzer

1  Package compile; 2 3  Public class Cifa {4   int A; 5   int c = a+1; 6 }      

The parsing result of the lexical analyzer is to match all the keywords in this class to any of the items in the token class. The matching results of the above code are as follows

4. Parser

The function of the lexical analyzer is to convert the character flow of the Java source file into the corresponding token stream, and the parser is to build the token stream into a more structured syntax tree, that is, to assemble the words into a single sentence.

1  Public classyufa{2   intA;3   Private intC =a +1;4   5    Public intGetC () {6       returnC;7   }8 9    Public voidSETC (intc) {Ten       This. C =C; One   } A}

This code corresponds to the syntax tree

5. Semantic Analyzer

The syntax tree generated by the parser is too coarse, it needs to do some processing on the basis of this syntax tree, such as adding constructors to the class, checking whether the variables are initialized before use, merging some constants, checking whether the action variable type matches, checking whether all the operation statements are up to, Check if the checked exception exception has been caught or thrown, touches Java syntax sugar (generics, unpacking), removes some never-true conditions, and the inner class is detached but holds a reference to an external class

6. Code generator

The code blocks in the Java method are converted into command forms that conform to the JVM syntax, and the JVM operates on a stack-based basis, and all operations must be done through the stack and the stack.

Output bytecode to a file in class extension according to the JVM's file organization format

Javac Compilation principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.