On the inlet java program main () method

On the inlet java program main () method

The method main () method signature

public static void main(String[] args)

Explain the method signature

 public modifier: java class called by the java virtual machine (JVM), in order not to limit free to call, so the use of public modifier.
 static modifier: Definitely not create JVM when you call the main method of the main class of the object, and then through the object to invoke methods, but directly through the class to call this method, it is necessary to use the static modifier modify this class.
 Return value void: the main method is invoked JVM, will return value to the JVM does not make any sense, so this method does not return a value, so use void.
 Extended:
 public: This modifier indicates that the data members, member functions are open to all users, all users can make calls directly.
 static: This modifier represents the static meaning, simply understood to be modified static modifier members belong to the class itself, rather than an instance belongs to the class, static members can not have direct access to non-static members.
 void: void Description Use no return value.

Then find that I did not say a string array parameter content, where we first know who is calling the method, who is going to pass parameter, so the args parameter responsible for the assignment by the JVM, JVM args assigned to what value?

We look at a program:

public class args {
    public static void main(String[] args){
        for (String s:args) {
            System.out.println(s);
        }
        System.out.println("-------------------" );
        System.out.println(args.length);
    }
}

Output:

-------------------
0

 Here the output of the array length is zero, because the program is not set to the args parameter value, it was not known JVM args array elements, then the length of a JVM is set to 0 args an array of arrays.
 If we use the dos command to run the program, plus one or more strings (separated by spaces) after the class name, then the JVM will turn these values assigned to args array.
E.g:

λ java lzjarg lzj why
lzj
why
-------------------
2

Guess you like

Origin www.cnblogs.com/lzjwhy/p/11221890.html