The main function of each component in-depth understanding
<span style= "White-space:pre" ></span>public static void Main (string[] agrs)
Main function: is a special function, as the entry of the program, can be called by the JVM
the definition of the main function:
Public: Represents the maximum access permission for the function
Static: Represents the main function with the loading of the class already exists
void: The main function does not have a specific return value
Main: Not a keyword, but a special word that can be identified by the JVM
(string[] args): The parameter of the function, the parameter type is an array of elements in the array, the string array. The length of this spatiotemporal array of main (string[] args) string array is 0, but can also be The parameter is passed to it when it is run.
The main function is fixed-format, the JVM recognizes
The main function can be overloaded, but the JVM only recognizes main (string[] args), and the others are general functions. The args knowledge array variable in this can be changed, and the other cannot be changed. A Java file can contain many classes, each of which has only one main function, but each Java file can contain more than one main function, at run time, you need to specify which JVM entry is. When a main function such as a class can call the main function of another class.
Case one: A Java file can contain multiple classes, each of which can contain only one main function. When a Java file contains multiple main functions, the program entry is specified.
<pre name= "code" class= "java" >class maindemo {public static void main (string[] args) {System.out.println ( Args.length)//system.out.println (args[1]);//This is the wrong System.out.println ("maindemo----0"); main (1);} public static void main (int x) {SYSTEM.OUT.PRINTLN ("Overloaded main function---" +x);}} Class MainDemo1 {public static void main (string[] args) {System.out.println (args.length);//system.out.println (Args[1] );//This is the wrong System.out.println ("Maindemo----1");}}
Case TWO: The program entry main function at execution time, you can pass to the same parameters as normal functions.
Class Maindemo{public static void Main (string[] args) {string[] arr={"haha", "hha", "Heihei", "Xixi", "Hiahia"}; Maintest.main (arr);}} Class Maintest{public static void Main (string[] args) {for (int x=0;x<args.length;x++) {System.out.println (args[x]);}}}
Re-recognize the components of the main function in Java