Java 数组参数声明语法“...”如何表示工作?

发布于 2024-10-28 05:11:39 字数 327 浏览 6 评论 0原文

写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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

知你几分 2024-11-04 05:11:39

我相信这是在 Java 1.5 中实现的。该语法允许您使用逗号分隔的参数列表而不是数组来调用方法。

public static void main(String... args);
main("this", "is", "multiple", "strings");

与:http://today 相同

public static void main(String[] args);
main(new String[] {"this", "is", "multiple", "strings"});

。 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.

public static void main(String... args);
main("this", "is", "multiple", "strings");

is the same as:

public static void main(String[] args);
main(new String[] {"this", "is", "multiple", "strings"});

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

陌上青苔 2024-11-04 05:11:39

查看 Java 语言规范,第三版,第 8 章(类) 。埋在里面的是这个金块:

如果最后一个形参是T类型的可变实数参数,则认为定义了T[]类型的形参。那么该方法是可变数量方法。否则,它是固定数量方法。变量数量方法的调用可能包含比形式参数更多的实际参数表达式。所有与变量参数之前的形式参数不对应的实际参数表达式都将被计算,并将结果存储到一个数组中,该数组将传递给方法调用(第 15.12.4.2 节)。

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

所以基本上,你所拥有的是一种复制更传统的奇特方式

String[] args

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

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).

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
岁月染过的梦 2024-11-04 05:11:39

它是 varargs

简单来说,它是一个 MemberArray,例如

public setMembers(Member[] members);

何时使用:

通常在设计 API 时,当参数数量为不固定。

标准 API 的示例是 String。格式(字符串格式,对象...参数)

另请参见

It is varargs

In simple term its an Array of Member like

public setMembers(Member[] members);

When 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

南风几经秋 2024-11-04 05:11:39

这就是所谓的 varargs 语法。在方法主体中,您可以读取 members 参数,因为它是一个数组 - 实际上,它“只是”一个数组。

然而,神奇之处在于调用该方法。在引入 varargs 语法之前,您可以像这样调用该方法:

setMembers(new Members[] {member1, member2, member3});

但是,使用新的 varargs 语法,您不再需要显式创建数组,并且可以传递

setMembers(member1, member2, member3);

:然而,这确实意味着 varargs 参数必须是方法中的最后一个参数。因此,这样的事情是不允许的:

void setMembers(Member ... members, String memberType);

总结:这确实是一点语法糖。我不是 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:

setMembers(new Members[] {member1, member2, member3});

With the new varargs syntax however, you don't need to explicitly create the array anymore, and you can pass:

setMembers(member1, member2, member3);

This does mean however that a varargs argument has to be the last argument in a method. Something like this is therefore not permitted:

void setMembers(Member ... members, String memberType);

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.

记忆里有你的影子 2024-11-04 05:11:39

它称为可变参数:一种可以接受任意数量(包括零)参数的函数。
例如,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 as main({"string1", "string2", "string3"}) if main is declared as void main(String...args).

See http://www.java-tips.org/blog/java-se/varargs-%E2%80%93-java-50-addition.html

还不是爱你 2024-11-04 05:11:39

这意味着您可以将零个或多个 Member 对象传递给 setMembers() 方法。在 setMembers 方法中,members 将具有数组语义。

It means you can pass zero or more Member objects to the setMembers() method. In the setMembers method, members will have array semantics.

菩提树下叶撕阳。 2024-11-04 05:11:39

变量参数。可以有 0 个或多个字符串参数。

该函数可以访问字符串数组形式的参数。

Variable arguments. Can have 0 or more String arguments.

The function can access the parameters as an array of Strings.

小糖芽 2024-11-04 05:11:39

这意味着该方法接受可变数量的字符串参数。参数被视为数组,因此可以按照传入的顺序通过下标进行访问。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文