main 方法是获取初始化数组还是命令行中的字符串在我键入时直接输入到参数中?

Does the main method get an initialized array or are the strings in the command line enter directly in to the parameter as I type?

例如,如果我想打印一个数组长度,我不能这样做:

public class Test{
     public static void main(String [] args){
          System.out.println(worngParam({"first", "second", "ect"}));
     }
     public static int worngParam(String [] strings){
          return strings.length;
     }
}

这是一个错误!

main中的前两行必须是

String [] strings = {"first", "second", "ect"};
System.out.println(worngParam(strings));

但即使这样我也能做到:

System.out.println(args.length);//If of course args is not empty

我的问题是参数如何进入main方法?

尽管任何方法都可以接受常量变量,例如3, "word", 'a'。但是她无法像这样 {1,8}{"word2", "word3"}

初始化数组

假设您 运行 您的 java class 文件是这样的

javac Test.java
java -cp . Test firstParam secondParam thirdParam

然后在您的主要方法中,args 的值将类似于

args = new String[]{"firstParam", "secondParam", "thirdParam"};