Java--main () method
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:
/**
* Detailed description of main () method in Java
*/
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 isvoid。
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 Exception
Therefore, 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:
public class Testmain {
public static void Main (string[] args) throws Exception {
System.out.println ("hahaha haha");
throw new Exception ("");
}
}
Operation 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 1
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.
**
* Print the input parameters in the Main method
*/
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
Copy Code
V. Give another version of HelloWorld
/**
* Abnormal version of HelloWorld. hehe
*/
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.
The Java Main method explains