Java--The entire process of code compilation and execution

Source: Internet
Author: User

1. What is a JVM

The JVM is the core and foundation of Java, the virtual processor between the Java compiler and the OS platform. It is an abstract computer based on the lower-level operating system and hardware platform and utilizes the Software method to implement the Java bytecode program. The Java compiler only needs to target the JVM to generate code or bytecode files that the JVM can understand. The Java source file is compiled into a bytecode program, which translates each instruction into a different platform machine code through the JVM and runs through a specific platform.

In short, the JVM is the equivalent of a diesel engine, it can only run in Java (Diesel), the JVM is a Java virtual machine, with the JVM to run Java programs

2. Java code compilation is done by the Java source compiler, and the flowchart is as follows:

The execution of Java bytecode is done by the JVM execution engine, and the flowchart is as follows:

The entire process of compiling and executing Java code consists of the following three important mechanisms:

    • Java source Code compilation mechanism
    • Class loading mechanism
    • Class execution mechanism

3.Java source Code compilation mechanism

Java source code compilation consists of the following three processes:

    • Parse and input to symbol table
    • Annotation processing
    • Semantic analysis and generation of class files

The flowchart is as follows:

The resulting class file is made up of the following sections:

    • Structure information. Includes the class file format version number and the number and size of each part of the information.
    • Meta data. The information that corresponds to the declarations and constants in the Java source code. Contains the declaration information, domain and method declaration information, and constant pool of classes/inherited superclass/implemented interfaces.
    • Method information. Corresponds to the information in the Java source code for statements and expressions. Includes byte code, exception Processor table, evaluation stack and local variable size, type record of evaluation stack, debug symbol information.

4. Class loading mechanism

The class loading of the JVM is done through ClassLoader and its subclasses, and the hierarchy and loading order of the classes can be described as follows:

  

1) Bootstrap ClassLoader

Responsible for $JAVA_HOME中jre/lib/rt.jar all the classes in the load, implemented by C + +, not ClassLoader subclasses.

2) Extension ClassLoader

Some jar packages that are responsible for loading the extended functionality in the Java platform, including $JAVA_HOME中jre/lib/*.jar or -Djava.ext.dirs specifying the jar packages in the directory.

3) App ClassLoader

Responsible for documenting the jar packages specified in Classpath and the class in the catalog.

4) Custom ClassLoader

ClassLoader, such as Tomcat and JBoss, are self-implementing ClassLoader according to the Java EE specification, which is customized by the application.

The load process checks to see if the class is loaded, the check order is bottom-up, the custom ClassLoader to BootStrap ClassLoader-by-layer check, as long as a certain ClassLoader has been loaded as if this class is loaded, guaranteeing that this class is only Classloade R is loaded once. The order of loading is top-down, that is, the upper layer tries to load the class one at a level.

5. Class execution mechanism

The JVM is a stack-based architecture that executes class bytecode. After the thread is created, the program counter (PC) and stack (stack) are generated, and the program counter holds the offset of the next instruction to be executed in the method, and the stack frames are stored in each stack frame, and each stack frame corresponds to each call of each method, and the stack frame is made up of the local variable area and the operand stack. Local variables are used to store local variables and parameters in the method, which are used to store the intermediate results produced during the execution of the method. The structure of the stack is as follows:

Java--The entire process of code compilation and execution

Related Article

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.