Preface
We are very familiar with the main functions in JAVA. I believe that everyone who has learned the JAVA language can skillfully write the entry function of this program. However, for why the main functions are written in this way, what does each keyword mean? Perhaps not everyone can easily answer it. I also encountered this problem during my studies. By searching for information online and adding my own practice, I finally got some experience. I dare not keep it and wrote it out to share it with everyone.
main() method in Java
The java virtual machine finds the running program that needs to be started through the main method, and checks whether the class where the main function is located is loaded by the java virtual machine. If it is not loaded, then load the class and load all related other classes. Therefore, when the program is running, the first method to execute is main() method. Normally, if you want to run a class method, you must first instantiate an object of this class, and then pass " Object name. Method name() ", but because main is the entry point of the program, the object has not been instantiated at this time, the main method is declared as static, so that this method can be called directly through the " class name. method name() ".
Before looking at the main() method in Java, let’s first look at the simplest Java application HelloWorld. I will use this example to illustrate the mystery of main() method in the Java class. The code of the program is as follows:
/** * Detailed explanation of main() method in Java*/ public class HelloWorld { public static void main(String args[]) { System.out.println("Hello World!"); } }1. Let’s talk about the category first:
There is main() method in the HelloWorld class, which means that this is a java application that starts and runs directly through the JVM.
Since it is a class, Java allows classes to not be subject to public keyword constraints. Of course, the definition of the class can only be limited to public or unlimited keywords (the default).
2. Let’s talk about the main() method
The main() method is declared as: public static void main(String args[]) . It must be defined like this, this is the Java specification.
Why is it defined like this? It has something to do with the operation of the JVM.
When there is main() method in a class, executing the command "java class name" will start the virtual machine to execute the main method in the class.
Since the JVM will first call the main method when running this Java application. When calling, the object of this class is not instantiated, but is called directly through the class name, so it needs to be limited to public static.
For the main method in java, jvm has restrictions and cannot have a return value, so the return value type is void.
There is also an input parameter in the main method, which is type String[], which is also the Java specification. main() method must have an entry parameter, and the class details must be String[] . As for the name of the string array, this can be set by yourself. According to habits, the name of this string array is generally consistent with the name of the mian parameter in the sun java specification example, and is named args.
Therefore, main() method definition must be: " public static void main(String 字符串数组参数名[]) ".
3. The main() method can throw Exception
Therefore, an exception can be thrown in main() method, and an exception can also be declared on main() method.
For example, the following is correct:
public class TestMain { public static void main(String[] args) throws Exception { System.out.println("hahahahahahahaha"); throw new Exception(""); } }Running result: hahahahaha Exception in thread "main" java.lang.Exception: at maintainest.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
4. The function of string parameter array in main() method
In main() method, the string parameter array functions to receive command line input parameters, and the parameters of the command line are separated by spaces.
Here is an example to see how to initialize and use this array.
/*** Print input parameters in main method */ public class TestMain { public static void main(String args[]){ System.out.println("Print input parameters in main method!"); for(int i=0;i<args.length;i++){ System.out.println(args[i]); } } } Execution method and operation 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
5. Give another version of HelloWorld
/** * The perverted 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 content of the sentence " System.exit(0); ", with the purpose of making the program end normally. Where does "HelloWorld!" print from? The secret is printed in static, because the contents of the static code block will be called before the main call.
VI. Example
The virtual machine starts by calling the method main of a specified class, passing it to main a string array parameter, so that the specified class is loaded, linking other types used by the class, and initializing them. For example, for the program:
public class HelloApp { public static void main(String[] args) { System.out.println("Hello World!"); for (int i = 0; i < args.length; i++) { System.out.println(args); } }}After compilation, type in command line mode: java HelloApp run virtual machine
The Java virtual machine will be started by calling HelloApp's method main and passed to main an array containing three strings "run", "virtual", and "machine". Now we briefly describe the steps that virtual machines may take when executing HelloApp.
I started trying to execute the main method of HelloApp class and found that the class was not loaded, which means that the virtual machine does not currently contain the binary representative of the class. So the virtual machine used ClassLoader to try to find such a binary representative. If this process fails, an exception is thrown. After the class is loaded, before the main method is called, the class HelloApp must be linked to other types and initialized. The link consists of three stages: inspection, preparation and parsing. Verify the symbols and semantics of the loaded main class, prepare to create static domains of the class or interface and initialize these domains to standard default values, and parsing is responsible for checking the symbolic references of the main class to other classes or interfaces. It is optional at this step. Class initialization is the execution of the static initialization function and the initialization constructor of the static domain declared in the class. A class's parent class must be initialized before it is initialized.
The whole process is as follows:
Summarize
As a special specification, the main method is very different from ordinary methods and has many limitations. Understanding its principles requires learning JVM-related knowledge. It is a major obstacle to learning in Java. The above is the summary of the principle and use of main compiled by the editor. I hope it will be helpful to you. If you have any questions, you can leave a message to communicate.