如何在Java数组参数声明语法" ..."工作? [英] How does the Java array argument declaration syntax "..." work?

查看:119
本文介绍了如何在Java数组参数声明语法" ..."工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在写Java的一段时间,今天我遇到了以下声明:

I have been writing java for a while, and today I encountered the following declaration:

public static void main(String... args) {

}

请注意在数组声明中的点点点,而不是通常的支架[]。显然,它的工作原理。事实上,我写了一个小测试,并验证它的工作原理。所以,我把Java的语法,看看这个语法参数声明的是,却没有发现任何东西。

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.

推荐答案

查看 Java语言规范,第三版,第8章(类)。埋在有这块金块:

Check out the Java Language Specification, Third Edition, Chapter 8 (Classes). Buried in there is this nugget:

如果在最后一次正式参数的类型T的变量参数元数,它被认为是定义一个类型T []的形式参数。该方法是再一变元数的方法。否则,它是一个固定的元数的方法。可变元数方法的调用可能包含更多的实际参数前pressions比形式参数。所有实际参数前$ P $不符合形式参数preceding可变参数元数将pssions进行评估,并把结果存储到将要传递给方法调用数组(§15.12.4.2)。

If the last formal parameter is a variable arity parameter of type T, it is considered to define a formal parameter of type T[]. The method is then a variable arity method. Otherwise, it is a fixed arity method. 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 (§15.12.4.2).

基本上,任何方法调用的最后一个参数可以有 T ... 。如果有,它被转换为 T []

Basically, the last parameter of any method call can have T.... If it has that, it is converted to T[].

所以基本上,你所拥有的是再现一个奇特的方式更为传统

So basically, what you have is a fancy way of reproducing the more traditional

String[] args

这篇关于如何在Java数组参数声明语法" ..."工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆