the main () method in Java goes from http://lavasoft.blog.51cto.com/62575/53263In 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:/**
* Detailed description of main () method in Java
*/
PublicclassHelloWorld {
PublicStaticvoidMain (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 () methodThe 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, the main () method can throw ExceptionTherefore, the main () method can throw an exception, and the main () method can also declare an exception to be thrown. For example, the following is the correct wording: PublicclassTestmain {
Public StaticvoidMain (string[] args)throwsException {
System.out.println ("hahaha");
ThrowNewException ("");
}
Run Result: hahaha 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.execution.application.AppMain.main (appmain.java:90)
Process finished with exit code 1iv. function of string parameter array in main () methodThe 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./**
* Print the input parameters in the Main method
*/
PublicclassTestmain {
PublicStaticvoidMain (String args[]) {
System.out.println ("Print the input parameters in the Main method!" ");
for(inti=0;i<args.length;i++) {
System.out.println (Args[i]);
}
}
Execute method and run 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
3v. Give another version of HelloWorld /**
* Abnormal version of HelloWorld. hehe
*/
PublicclassHelloWorld2 {
Static{
System.out.println ("Hello wordld!");
}
PublicStaticvoidMain (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.Summary: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 is my summary of the principle and use of main, you are welcome to discuss here.
A detailed description of the main () method in Java