JVM Learning Notes-java The process of compiling and running a program

Source: Internet
Author: User

The whole process of compiling and running Java is quite tedious, this article uses a simple program to explain the whole process.
Java programs are created from source files to program runs in two major steps:
1, the source file compiled by the compiler into a byte code (bytecode)
2. Bytecode is run by Java Virtual Machine interpretation .
Because Java programs are compiled and run by the JVM's interpretation, Java is called a semi explanatory language .

The following Java program is used to illustrate the entire flow of Java programs from compile to last run. The code is as follows:

Mainapp.java public
class Mainapp {public
    static void Main (string[] args) {
        Animal Animal = new Animal ("Pup Py ");
        Animal.printname ();
    }
Animal.java public
class Animal {public
    String name;
    Public Animal (String name) {
        this.name = name;
    }
    public void Printname () {
        System.out.println ("Animal [" +name+ "]");
    }

First step (Compile):
After the source file has been created, the program is compiled to the. class file first. When Java compiles a class, if the class that the class relies on has not been compiled, the compiler compiles the dependent class and then references it, otherwise it's like make. If the Java compiler cannot find a. class file or A. Java source file for the class on which the class depends, the compiler cant the error of the "Find Symbol" in the specified directory. The compiled bytecode file format is mainly divided into two parts: the constant pool and the method byte code . The constant pool records all the token (class name, member variable name, and so on) that the code appears in, as well as the symbolic reference (method reference, member variable reference, and so on); The method byte is the byte code of each method in the class. The following is the result of mainapp.class through disassembly, and we can see clearly the structure of the. class file:
Mainapp class constant Pool

Mainapp class constant Pool

Step Two (Run):
The process of running Java class can be divided into two processes: 1, class loading 2, class execution. It is important to note that the JVM will only load the class when it is first actively using the class. That is, the JVM does not load a program into memory in the first place, but only loads it when it has to be used, and loads it only once.
The following are detailed steps for the program to run:
After compiling the Java program to get the Mainapp.class file, knock the Java appmain on the command line. The system initiates a JVM process, where the JVM process finds a binary file named Appmain.class from the classpath path, loading the Mainapp class information into the method area of the Run-time data area, which is called the Mainapp class load.
The JVM then finds the Appmain main function portal and starts executing the main function.
The first command of the main function is animal animal = new Animal ("Puppy"), which means that the JVM creates a animal object, but this time the method area does not have the animal class information, so the JVM loads the animal class immediately. Put the type information of the animal class in the method area.
After loading the animal class, the first thing the Java Virtual machine does is allocate memory for a new animal instance in the heap area, and then call the constructor to initialize the animal instance. This animal instance holds a reference to the type information of the animal class that points to the method area (which contains the method table, the underlying implementation of the Java dynamic binding).
When using Animal.printname (), the JVM finds the animal object based on the animal reference, and then obtains the animal () by locating the method table of the type information of the Printname class in the method area, based on the reference held by the animal object. The address of the byte code of the function.
Start running the Printname () function.

Java program running Process

Special note: All public and protected instance methods in a Java class adopt dynamic binding mechanisms, all private methods, static methods, constructors, and initialization methods are static binding mechanisms. The method table is used when the dynamic binding mechanism is used, which is not used when the static bindings are applied. This article is only about the process of running the Java program, so there is no fine distinction. The process described in this article is very rough, and readers who want to know more about it are kindly consulted for additional information. Where there is a fallacy, please correct me.

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.