Main function parsing in Java

Source: Internet
Author: User

From the time Java was written, the most likely to be written is the main function.

public static void main(String[] args) {}

However, I have never asked myself why I should write it like this, because there is no such requirement in C language. In fact, this is a problem that does not need to be explained, because the java standard is defined in this way, so since it is defined in the java standard, we can simply implement it according to the regulations. However, this is not a good learning attitude. If you always know it, but do not know why, there will always be a sense of separation for Java. It is to find the problem, not to solve it, not to understand why, there is always a worry. Today, I have asked myself why Java's main functions are written in this format, so I have to understand why.

In Java, the main () method is the entry method of Java applications. The Java virtual machine uses the main method to locate the running program to be started, and checks whether the class of the main function is loaded by the Java Virtual Machine. If it is not loaded, the class is loaded and all other related classes are loaded. Therefore, when the program is running, the first method to be executed is the main () method. Generally, if you want to run a class method, you must first instantiate an object of the class and then use the "Object Name. method Name () "to run the method, but because main is the entry of the program, the object has not yet been instantiated, so the main method is declared as static, in this way, you can directly use the class name. method.

Instance

The VM starts by calling the main method of a specified class and passes it to main a string array parameter to load the specified class, link other types used by the class, and initialize them. For example, for programs:

public 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);        }    }}

After compilation, type: Java helloapp run virtual machine in command line mode.

The Java Virtual Machine is started by calling the helloapp method main and passed to main an array containing three strings: "run", "virtual", and "machine. The following describes the steps that a VM may take when executing helloapp.

He started to execute the main method of the helloapp class and found that the class was not loaded. That is to say, the virtual machine does not currently contain the binary representation of the class, so the virtual machine uses classloader to find such a binary representation. If the process fails, an exception is thrown. Before the main method is called, the class helloapp must be linked to other types and initialized. The Link contains three phases: Test, preparation, and resolution. Check the symbols and semantics of the loaded main class, and create static fields of the class or interface and initialize these fields as standard default values, resolution checks the symbolic references of the main class to other classes or interfaces. This step is optional. Class initialization is the execution of the static initialization function declared in the class and the initialization constructor of the static domain. The parent class of a class must be initialized before initialization. The process is as follows:

 

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.