Java 数组参数声明语法“...”如何表示工作?
写java有一段时间了,今天遇到了下面的声明:
public static void main(String... args) {
}
注意数组声明中的“点点点”,而不是通常的括号[]。显然它有效。事实上,我编写了一个小测试并验证了它的工作原理。于是,我拉了java语法看看这个参数声明的语法在哪里,但是没有找到任何东西。
那么请教各位专家,这是如何运作的呢?它是语法的一部分吗?另外,虽然我可以像这样声明函数,但我不能像这样在函数体内声明数组。
不管怎样,你知道有什么地方有这个记录吗?这是好奇心,也许不值得投入任何时间,但我被难住了。
I have been writing java for a while, and today I encountered the following declaration:
public static void main(String... args) {
}
Note the "dot dot dot" in the array declaration, rather than the usual bracket []. Clearly it works. In fact I wrote a small test and verified it works. So, I pulled the java grammar to see where this syntax of argument declaration is, but did not find anything.
So to the experts out there, how does this work? Is it part of the grammar? Also, while I can declare function like this, I can't declare an array within a function's body like this.
Anyway, do you know of any place that has this documented. It is curiosity, and perhaps not worth of any time invested in it, but I was stumped.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我相信这是在 Java 1.5 中实现的。该语法允许您使用逗号分隔的参数列表而不是数组来调用方法。
与:http://today 相同
。 java.net/article/2004/04/13/java-tech-using-variable-arguments
http://download.oracle.com/javase/1.5 .0/docs/guide/language/varargs.html
I believe this was implemented in Java 1.5. The syntax allows you to call a method with a comma separated list of arguments instead of an array.
is the same as:
http://today.java.net/article/2004/04/13/java-tech-using-variable-arguments
http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html
查看 Java 语言规范,第三版,第 8 章(类) 。埋在里面的是这个金块:
基本上,任何方法调用的最后一个参数都可以是
T...
。如果有,则将其转换为T[]
。所以基本上,你所拥有的是一种复制更传统的奇特方式
Check out the Java Language Specification, Third Edition, Chapter 8 (Classes). Buried in there is this nugget:
Basically, the last parameter of any method call can have
T...
. If it has that, it is converted toT[]
.So basically, what you have is a fancy way of reproducing the more traditional
它是
varargs
简单来说,它是一个
Member
的Array
,例如何时使用:
通常在设计 API 时,当参数数量为不固定。
标准 API 的示例是
String。格式(字符串格式,对象...参数)
另请参见
It is
varargs
In simple term its an
Array
ofMember
likeWhen to use:
Generally while designing API it is good to use when number of argument is not fixed.
Example from standard API of this is
String.format(String format,Object... args)
Also See
这称为可变参数,请检查此处:
官方文档(Java 1.5)在这里:
This is called variadic arguments, check here:
The official documentation (Java 1.5) is here:
这就是所谓的 varargs 语法。在方法主体中,您可以读取
members
参数,因为它是一个数组 - 实际上,它“只是”一个数组。然而,神奇之处在于调用该方法。在引入 varargs 语法之前,您可以像这样调用该方法:
但是,使用新的 varargs 语法,您不再需要显式创建数组,并且可以传递
:然而,这确实意味着
varargs
参数必须是方法中的最后一个参数。因此,这样的事情是不允许的:总结:这确实是一点语法糖。我不是 Java 编译器内部工作原理的专家,但我很确定调用接受 varargs 参数的方法的方法会被重新构建为构建给定类型数组的方法。
It's the so-called varargs syntax. In the method body, you can read the
members
parameter as it were an array - actually, it /is/ 'just' an array.However, the magic bit is in calling the method. Before the
varargs
syntax was introduced, you'd call the method a bit like so:With the new varargs syntax however, you don't need to explicitly create the array anymore, and you can pass:
This does mean however that a
varargs
argument has to be the last argument in a method. Something like this is therefore not permitted:Summarized: It's a bit of syntactic sugar, really. I'm no expert on the inner workings of the Java compiler, but I'm pretty sure that methods calling a method that accept a
varargs
parameter are rebuilt into methods that build an array of the given type.您可能想阅读 使用变量参数 (或 varargs)在 Java 中。
You might want to read up on Using Variable Arguments (or varargs) in Java.
它称为可变参数:一种可以接受任意数量(包括零)参数的函数。
例如,
main("string1", "string2", "string3")
与main({"string1", "string2", "string3"})
相同如果 main 被声明为void main(String...args)
。请参阅 http:// /www.java-tips.org/blog/java-se/varargs-%E2%80%93-java-50-addition.html
It's called varadic argument: A function that takes as many (including zero) arguments as you want.
For example,
main("string1", "string2", "string3")
is same asmain({"string1", "string2", "string3"})
if main is declared asvoid main(String...args)
.See http://www.java-tips.org/blog/java-se/varargs-%E2%80%93-java-50-addition.html
这意味着您可以将零个或多个
Member
对象传递给setMembers()
方法。在 setMembers 方法中,members
将具有数组语义。It means you can pass zero or more
Member
objects to thesetMembers()
method. In the setMembers method,members
will have array semantics.变量参数。可以有 0 个或多个字符串参数。
该函数可以访问字符串数组形式的参数。
Variable arguments. Can have 0 or more String arguments.
The function can access the parameters as an array of Strings.
这意味着该方法接受可变数量的字符串参数。参数被视为数组,因此可以按照传入的顺序通过下标进行访问。
It means that the method accepts a variable number of String arguments. The arguments are treated as an array and so are accessed by subscript, in the order they are passed in.