Detailed description of the main () method in Java

Source: Internet
Author: User
In Java, the main () method is the entry method of the Java application. That is to say, when the program is running, the first method to be executed is the main () method, this method is very different from other methods. For example, the method name must be main, the method must be public static void, and the method must receive parameters of a string array.

Before reading the main () method in Java, Let's first look at helloworld, the simplest Java application. I will use this example to illustrate the mysteries of the main () method in Java classes, the program code is as follows:

/**
* Created by intellij idea.
* User: leizhimin
* Date: 2007-12-3
* Time: 8:46:50
* Detailed description of the main () method in Java
*/
Public class helloworld {
Public static void main (string ARGs []) {
System. Out. println ("Hello world! ");
}
}

1. First class:

The helloworld class contains the main () method, which indicates that this is a Java application and runs the program directly through JVM.
Since it is a class, Java allows the class to be free from public keyword constraints. Of course, the class definition can only be public or unrestricted keywords (default ).

Ii. Let's talk about the main () method.

The declaration of this main () method is: public static void main (string ARGs []). This is a Java specification.

Why is this definition related to JVM running.
If a class contains the main () method and the "Java class name" command is executed, the VM is started to execute the main method in the class.

When running this Java application, JVM will first call the main method. When calling this method, the object of this class is not instantiated, but directly called by class name. Therefore, it must be limited to public static.

JVM has restrictions on the main method in Java and cannot return values. Therefore, the return value type is void.
The main method also has an input parameter of the string [] type, which is also a Java specification. The main () method must have an input parameter, and the class must be a string [], as for the name of the string array, this can be set by yourself. According to the habit, the name of this string array is generally the same as that of the mian parameter in the Sun Java standard example, named args.

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

3. The main () method does not allow throws exception.

Therefore, the exception in the main () method is either handled or not handled, and cannot be throttled.

For example, it is incorrect to write a public static int main (string ARGs []) throws exception to define the main method.

Iv. Functions of the string parameter array in the main () method

In the main () method, the string parameter array is used to receive the input parameters of the command line. The parameters of the command line are separated by spaces.

The following example shows how to initialize and use the array.
/**
* Created by intellij idea.
* User: leizhimin
* Date: 2007-12-3
* Time: 10:22:15
* Print the input parameters in the main method.
*/
Public class testmain {
Public static void main (string ARGs []) {
System. Out. println ("print the input parameters in the main method! ");
For (INT I = 0; I <args. length; I ++ ){
System. Out. println (ARGs [I]);
}
}
}

Execution method and result
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

5. Another version of helloworld is provided.

/**
* Created by intellij idea.
* User: leizhimin
* Date: 2007-12-3
* Time: 10:28:58
* Changed version helloworld.
*/
Public class helloworld2 {
Static {
System. Out. println ("Hello wordld! ");
}
Public static void main (string ARGs []) {
System. Exit (0 );
}
}

The content of the main () method is "system. Exit (0);", in order to let the program end normally. That "helloworld !" The secret is static because the content of the static code block is called before the main call.

Summary:
The main method, as a special specification, is very different from common methods and has many limitations. To understand its principles, you need to learn JVM knowledge. Is a major obstacle in Java learning.

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.