Java 区别 fn(String... args) 与 fn(String[] args)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/301563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
difference fn(String... args) vs fn(String[] args)
提问by Nrj
Whats this syntax useful for :
这个语法有什么用:
function(String... args)
Is this same as writing
这和写作一样吗
function(String[] args)
with difference only while invoking this method or is there any other feature involved with it ?
仅在调用此方法时有所不同,还是有任何其他功能与之相关?
采纳答案by bruno conde
The only difference between the two is the way you call the function. With String var args you can omit the array creation.
两者之间的唯一区别是调用函数的方式。使用 String var args,您可以省略数组创建。
public static void main(String[] args) {
callMe1(new String[] {"a", "b", "c"});
callMe2("a", "b", "c");
// You can also do this
// callMe2(new String[] {"a", "b", "c"});
}
public static void callMe1(String[] args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
public static void callMe2(String... args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
回答by JesperE
On the receiver size you will get an array of String. The difference is only on the calling side.
在接收器大小上,您将获得一个字符串数组。区别仅在调用方。
回答by kgiannakakis
You call the first function as:
您将第一个函数称为:
function(arg1, arg2, arg3);
while the second one:
而第二个:
String [] args = new String[3];
args[0] = "";
args[1] = "";
args[2] = "";
function(args);
回答by michelemarcon
with varargs (String...
) you can call the method this way:
使用 varargs ( String...
) 你可以这样调用方法:
function(arg1);
function(arg1, arg2);
function(arg1, arg2, arg3);
You can't do that with array (String[]
)
你不能用数组 ( String[]
)
回答by Mecki
The difference is only when invoking the method. The second form must be invoked with an array, the first form can be invoked with an array (just like the second one, yes, this is valid according to Java standard) or with a list of strings (multiple strings separated by comma) or with no arguments at all (the second one always must have one, at least null must be passed).
区别仅在于调用方法时。第二种形式必须用数组调用,第一种形式可以用数组调用(就像第二种形式,是的,根据 Java 标准,这是有效的)或字符串列表(多个字符串用逗号分隔)或完全没有参数(第二个总是必须有一个,至少必须传递 null)。
It is syntactically sugar. Actually the compiler turns
它在语法上是糖。其实编译器转
function(s1, s2, s3);
into
进入
function(new String[] { s1, s2, s3 });
internally.
内部。
回答by kamal
class StringArray1
{
public static void main(String[] args) {
callMe1(new String[] {"a", "b", "c"});
callMe2(1,"a", "b", "c");
callMe2(2);
// You can also do this
// callMe2(3, new String[] {"a", "b", "c"});
}
public static void callMe1(String[] args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
public static void callMe2(int i,String... args) {
System.out.println(args.getClass() == String[].class);
for (String s : args) {
System.out.println(s);
}
}
}