我使用 public static void main(String arg[]) 创建了一个 Java 应用程序
或
public static void main(String[] arg)
但是昨天我发现,如果我使用 public static void main(String... args)
编译程序,这也完全可以正常工作。 为什么?
最佳答案
这是因为String...
将被转换为String[]
Invocations of a variable arity method may contain more actual argument expressions than formal parameters. All the actual argument expressions that do not correspond to the formal parameters preceding the variable arity parameter will be evaluated and the results stored into an array that will be passed to the method invocation.
在 Java 中声明 varargs
是一个编译时错误,如下所示:
String... abc={"abc","def"};
这是因为 varargs
可用作方法签名中的最后一个参数,如 jls 中所述。 , varargs
将被评估,结果将存储在数组中,然后传递给方法
关于java - java中的main方法为什么接受无效的字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23487376/