This article is well written and reprinted! You can first look at Baidu's answer:
This is the entry address of the java program. When the java virtual machine runs the program, the first thing it looks for is the main method. The function of the main() function in the C language is the same. Only the java program with the main() method can be used by java. The virtual machine is ready to run, which can be understood as the specified format
for the parameters and modifiers
in it. The access rights of the program represented by public: indicate that it can be referenced in any occasion, so that the java virtual machine can find the main() method. To run the javac program
static: indicates that the method is static, does not depend on the object of the class, and belongs to the class. When the class is loaded, the main() method is also loaded into the memory with the
void: main() method is not The main that needs to return the value
: by convention, the specified
String[] args: receive parameters from the console
Example analysis of what does public static void main(String args[]) mean in Java
Author: NW_KNIFE Font: [ Increase and decrease ] Type: Reprint time: 2015-12-09 I want to comment
The example of this article describes the ins and outs of public static void main(String args[]) in Java. Share for your reference, the details are as follows:
public static void main(String[] args)
This is definitely not out of thin air, nor is it an unreasonable dead rule, but a need for java program execution.
Before the jvm tries to run a class, it checks whether the class contains a special method. This method must be public so that it can be accessed anywhere. This method must be static, because this method cannot rely on any instance of the class to run, and non-static methods must create an instance of the class before running.
This method has no return value. Unlike C/C++ programs, java programs end in normal by default, so main does not return int. If you want to end the program abnormally, you can use System.exit(1).
This method must accept a variable number of String type parameters, because the operator may have to additionally run parameters. Such as java HelloWorld jack 100, where jack and 100 are running parameters.
Why String? Because String is universal. Any literal form can be interpreted as a String, but other types are not (such as jack cannot be interpreted as an integer or floating point number), so String is the most suitable for storing parameters. And because the number of parameters is not limited to one, an array is used, namely String[]. After Java 1.5, it can also be written as String..., indicating an indeterminate number.
As for the variable name of the parameter, it can be arbitrary, as long as the parameter is obtained according to the variable name inside the method. From the perspective of the function of the variable, it is of course the most appropriate to call it arguments or args.
As for why the name of this method must be main, there are historical reasons, because the earliest C used the main function as the program entry, and java followed this historical tradition.
Let's take a look at an example:
运行结果:
希望本文所述对大家Java程序设计有所帮助