In java we can define main()
method as both these ways.
在java中,我们可以将main()方法定义为这两种方式。
public static void main(String[] args) {
System.out.println("Hello World");
}
.
。
public static void main(String... args) {
System.out.println("Hello World");
}
Both method takes array
of String
arguments. Now consider following scenario.
这两种方法都接受字符串参数数组。现在考虑以下场景。
String[] arr=new String[10]; // valid
String... arr=new String[10];// invalid
Java
never allows to create an array like this wayString... arr=new String[10];
. But in above method implementation java
allows to do so. My question is how java
achieve this two different behavior for two scenarios?
Java从不允许创建这样的wayString数组…[10];arr =新字符串。但是在上面的方法实现中,java允许这样做。我的问题是java如何在两个场景中实现这两个不同的行为?
4 个解决方案
#1
4
...
is a syntax for method arguments and not for variable definitions. The name of this notation is varargs, which is self explanatory i.e variable number of arguments.
是方法参数的语法,而不是变量定义的语法。这个符号的名称是varargs,它是自解释的i。变量数目。
#2
1
Variable argument or varargs(...) in Java used to write more flexible methods which can accept as many argument as you need not for initialization.
在Java中,变量参数或varargs(…)用于编写更灵活的方法,可以接受尽可能多的参数,而不需要初始化。
#3
1
...
refers to varargs and its main intention to make method more readable.
…是指varargs及其主要意图使方法更易读。
void method(String... args) {
}
can be called as
可以被称为
method("a");
OR method("a", "b");
OR method("a", "b", "c");
方法(“a”);或方法(“a”、“b”);或方法(“a”、“b”、“c”);
I see no point in using it in variable declaration, we can't do much with
我认为在变量声明中使用它没有意义,我们做不了太多。
String... a = {"a", "b"}
An array can anyways be declared with dynamic size
数组无论如何都可以用动态大小声明
String[] arr = {"a"};
OR
或
String[] arr = {"a", "b"};
#4
0
You can use varargs in main
because a method declared with varargs (...) is bytecode compatible with a declaration of a method with an array argument (for backwards compatibility). That does not mean that the same syntax is allowed for type declarations.
您可以主要地使用varargs,因为使用varargs(…)声明的方法与使用数组参数的方法声明兼容(为了向后兼容)。这并不意味着类型声明允许使用相同的语法。
#1
4
...
is a syntax for method arguments and not for variable definitions. The name of this notation is varargs, which is self explanatory i.e variable number of arguments.
是方法参数的语法,而不是变量定义的语法。这个符号的名称是varargs,它是自解释的i。变量数目。
#2
1
Variable argument or varargs(...) in Java used to write more flexible methods which can accept as many argument as you need not for initialization.
在Java中,变量参数或varargs(…)用于编写更灵活的方法,可以接受尽可能多的参数,而不需要初始化。
#3
1
...
refers to varargs and its main intention to make method more readable.
…是指varargs及其主要意图使方法更易读。
void method(String... args) {
}
can be called as
可以被称为
method("a");
OR method("a", "b");
OR method("a", "b", "c");
方法(“a”);或方法(“a”、“b”);或方法(“a”、“b”、“c”);
I see no point in using it in variable declaration, we can't do much with
我认为在变量声明中使用它没有意义,我们做不了太多。
String... a = {"a", "b"}
An array can anyways be declared with dynamic size
数组无论如何都可以用动态大小声明
String[] arr = {"a"};
OR
或
String[] arr = {"a", "b"};
#4
0
You can use varargs in main
because a method declared with varargs (...) is bytecode compatible with a declaration of a method with an array argument (for backwards compatibility). That does not mean that the same syntax is allowed for type declarations.
您可以主要地使用varargs,因为使用varargs(…)声明的方法与使用数组参数的方法声明兼容(为了向后兼容)。这并不意味着类型声明允许使用相同的语法。