Main method parsing (with reference, for the novice explained in detail is the Main method)

Source: Internet
Author: User

In Java, the main () method is the entry method for a Java application, that is, when the program is running, the first method to execute is the main () method, which differs greatly from other methods, such as the name of the method must be main, and the method must be public For a static void type, the method must receive a string array of arguments, and so on.

Before looking at the main () method in Java, let's look at one of the simplest Java application HelloWorld, which I'll use to illustrate the mystery of the main () method in the Java class, and the code for the program is as follows:

public class HelloWorld {
public static void Main (String args[]) {
System.out.println ("Hello world!");
}
}

First, say the class:

The HelloWorld class has the main () method, which indicates that this is a Java application that starts the running program directly through the JVM.
Since it is a class, Java allows classes to be non-public keyword constraints, although the definition of a class can only be restricted to public or unrestricted keywords (default).

Second, the main () method

The declaration of this main () method is: public static void Main (String args[]). This must be defined, which is the Java specification.

Why this is defined is related to the operation of the JVM.
When there is a main () method in a class, executing the command "Java class name" will start the virtual machine to execute the main method in that class.

Since the JVM is running this Java application, the main method is called first, and the object of the class is not instantiated, but is called directly by the class name and therefore needs to be restricted to public static.

For the main method in Java, the JVM has a limit and cannot have a return value, so the return value type is void.
The main method also has an input parameter, type string[], this is the Java specification, the main () method must have a parameter, the class must be string[], as for the name of the string array, this can be set by itself, according to the habit, The name of this string array is generally consistent with the Mian parameter name in the Sun Java specification paradigm, named args.

Therefore, the main () method definition must be: "public static void main (string string array parameter name [])".

Three, Main () method is not allowed throws Exception

Therefore, the exception in the main () method is either handled or not processed and cannot continue to be thrown.

For example, write a public static int main (String args[]) throws exception to define the main method is wrong.

Iv. function of string parameter array in main () method

The function of the string parameter array in the main () method is to receive command line input parameters, separated by a space between the parameters of the command line.

Here's an example to see how to initialize and use this array.

public class Testmain {
public static void Main (String args[]) {
System.out.println ("Prints the input parameters in the Main method! ");
for (int i=0;i<args.length;i++) {
System.out.println (Args[i]);
}
}
}

Execution methods and run results
D:\study\basetest\src>javac Testmain.java

D:\study\basetest\src>java Testmain 1 2 3
Print the input parameters in the Main method!
1
2
3

V. Give another version of HelloWorld

public class HelloWorld2 {
static {
System.out.println ("Hello wordld!");
}
public static void Main (String args[]) {
System.exit (0);
}
}

The main () method executes the phrase "system.exit (0);", to allow the program to end normally. That "helloworld! "Where is it printed, the secret is printed in static, because the contents of the static code block are called before the main call.

Summarize:
The main method, as a special specification, is very different from the common method and has many limitations, so it is necessary to learn the knowledge of the JVM by understanding its principle. is a major obstacle in learning in Java.

This article is from the "11923789" blog, please be sure to keep this source http://11933789.blog.51cto.com/11923789/1834531

Main method parsing (with reference, for the novice explained in detail is the Main method)

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.