Deep understanding of the main () method in Java

Source: Internet
Author: User

Deep understanding of the main () method in Java
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 :/**
* Detailed description of the main () method in Java
*/
Public class HelloWorld {
Public static void main (String args []) {
System. out. println ("Hello World! ");
}
} 1. Class 1: 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 ). 2. The declaration of the 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. throw Exception can be thrown in the main () method, so an Exception can be thrown in the main () method, and an Exception can be declared in the main () method. For example, the following statement is correct: public class TestMain {
Public static void main (String [] args) throws Exception {
System. out. println ("Haha ");
Throw new Exception ("");
}
} Running result: Haha
Exception in thread "main" java. lang. Exception:
At maintest. TestMain. main (TestMain. java: 11)
At sun. reflect. NativeMethodAccessorImpl. invoke0 (Native Method)
At sun. reflect. NativeMethodAccessorImpl. invoke (NativeMethodAccessorImpl. java: 39)
At sun. reflect. DelegatingMethodAccessorImpl. invoke (DelegatingMethodAccessorImpl. java: 25)
At java. lang. reflect. Method. invoke (Method. java: 585)
At com.intellij.rt.exe cution. application. AppMain. main (AppMain. java: 90)

Process finished with exit code 1 4. Function of the string parameter array in main () method the function of the string parameter array in main () method is to receive the input parameters of the command line, command line parameters are separated by spaces. The following example shows how to initialize and use the array. /**
* 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 running 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. provide another version of HelloWorld /**
* 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. This is my summary of the main principle and usage. You are welcome to discuss it here.

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.