Java virtual machine is running Shangxue Tang Xian

On the face of each portion of the virtual machine is a more detailed explanation, to analyze its operation procedure by a specific example.

Virtual machine main started by calling a method of the specified class, passing a string array to the main parameters to the specified class to be loaded, while the other type of link used class and initialize them. For example, the program:

class HelloApp

{

public static void main(String[] args)

{

System.out.println("Hello World!");

for (int i = 0; i < args.length; i++ )

{

System.out.println(args[i]);

}

}

}

Compiled at the command line mode, type: java HelloApp run virtual machine

HelloApp will be started by calling the method main java virtual machine, transfer to a main string that contains three "run", "virtual", "machine" of the array. Now we outline the steps in the implementation of the virtual machine HelloApp might take.

Began trying to execute the main method of class HelloApp it found that the class has not been loaded, which means that the virtual machine does not currently contain binary on behalf of the class, so the virtual machine uses ClassLoader trying to find such binary representative. If this process fails, an exception is thrown. Meanwhile, before main it is called, and the link must be initialized with other types of post-class HelloApp class is loaded. Link consists of three phases: inspection, preparation and resolution. The main class of tests to check loaded symbols and semantics, ready to create a class or a static domain interfaces, and these fields are initialized to standard default values, resolve is responsible for checking the main class reference to symbol other class or interface, at this stage it It is optional. Initialize static class initialization function is to perform the initialization configuration of the method and the static fields declared in the class. A class initialization before its parent class must be initialized. The whole process is as follows:

Java virtual machine is running Shangxue Tang Xian

Guess you like

Origin blog.51cto.com/14512197/2439026