Java compile-time and runtime & JVM

Source: Internet
Author: User

The whole process of compiling and running Java is quite tedious, and this article simply explains the whole process through a simple program.

The first two graphs describe the process of compiling and executing:

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:

For example, a Java program is created from a source file to a program that takes two major steps: 1, the source file is compiled by the compiler into bytecode (bytecode) 2, and the bytecode is run by the Java Virtual machine interpretation. Because Java programs are compiled and run by the JVM's interpretation, Java is called a semi-explanatory language ("semi-interpreted" language).


Figure 1 Java program compilation run process

The following is a Java program that illustrates the entire process of Java programs from compilation to Last run. The code is as follows:

Java code
  1. Mainapp.java
  2. public class Mainapp {
  3. public static void Main (string[] args) {
  4. Animal Animal = new Animal ("Puppy");
  5. Animal.printname ();
  6. }
  7. }
  8. Animal.java
  9. public class Animal {
  10. public String name;
  11. Public Animal (String name) {
  12. THIS.name = name;
  13. }
  14. public void Printname () {
  15. System.out.println ("Animal [" +name+ "]");
  16. }
  17. }

First step (compile): After the source file is created, the program is first compiled into a. class file. When Java compiles a class, if the class depends on the classes that have not yet been compiled, the compiler will compile the dependent class and then reference it, or else direct reference, which is a bit like make. If the Java compiler cannot find the. class file or the. Java source file of the class to which the class is dependent under the specified directory, the compiler is called "Cant find Symbol" error.

The compiled bytecode file format is mainly divided into two parts: the constant pool and the method byte code. A constant pool records all tokens (class names, member variable names, and so on) that have occurred in the code, as well as symbolic references (method references, member variable references, and so on); The method byte code is the bytecode of each method in the class. Here's mainapp.class. The structure of the. class file can be clearly seen through disassembly: Figure 2 Mainapp class constant pool
Figure 3 Mainapp class method byte code

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 class/inherited superclass/implemented interface declaration information, domain and method declaration information, and constant pool
    • 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

Second step (run): Java class running process can be divided into two procedures: 1, class loading 2, the execution of the class. It is important to note that the JVM will not load the class until the first time the program has actively used the class.        That is, the JVM does not load a program into memory in the first place, but loads it only once when it has to be used. Here are the detailed steps to run the program:
    1. After compiling the Java program to get the Mainapp.class file, tap Java appmain on the command line. The system starts a JVM process, and the JVM process finds a binary file named Appmain.class from the classpath path and loads the Mainapp class information into the method area of the runtime data area, a process called Mainapp class loading.
    2. The JVM then finds the main function entry for Appmain and begins executing the main function.
    3. The first command of the main function is animal animal = new Animal ("Puppy"), which is to have the JVM create a Animal object, but this time there is no information about the animal class in the method area, so the JVM immediately loads the animal class, Put the type information of the animal class into the method area.
    4. 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 for the animal class of the method area, which contains the method table, the underlying implementation of the Java dynamic binding.
    5. When using Animal.printname (), the JVM finds the animal object based on the animal reference and then navigates to the method table for the type information of the animal class in the method area based on the reference held by the animal object, obtaining Printname () The address of the byte code of the function.
    6. Start running the Printname () function.

Figure 4 Java program running process Special Description: All public and protected instance methods in Java class adopt dynamic binding mechanism, all private methods, static methods, constructors and initialization method <clinit> are all using static binding mechanism. The method table is used when the dynamic binding mechanism is used, and the static bindings are not used.

Ps:

Method Overloading: This occurs at compile time. Method overloading is also known as compile-time polymorphism, because the compiler can choose which method to use based on the type of the parameter.

1234 publicclass {     public static void evaluate(String param1);  // method #1     public static void evaluate(intparam1);   // method #2}

If the compiler wants to compile the following statement:

1 evaluate(“My Test Argument passed to param1”);

It generates a byte code that calls the # # method, based on the parameters passed in as String constants

Method overrides: This occurs at run time. The method overload is called run-time polymorphism because the compiler does not know at compile time and cannot know which method to invoke. The JVM will make a decision when the code is running.

123456789101112 publicclassA {   publicintcompute(int input) {          //method #3        return3* input;   }       }publicclassextendsA {   @Override   publicintcompute(intinput) {          //method #4        return4* input;   }       }

Compute (..) in sub-category B The Compute method overrides the parent class's (..) Method. If the compiler encounters the following code:

123 publicint evaluate(A reference, int arg2)  {     intresult = reference.compute(arg2);}

The compiler is not able to know whether the type of the passed parameter reference is a or B. Therefore, it is only possible at run time to determine whether to invoke method # or method # #, depending on the type of object assigned to the input variable "reference" (for example, an instance of a or B).

Java compile-time and runtime & JVM

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.