Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
86 views
in Technique[技术] by (71.8m points)

How does the Java array argument declaration syntax "..." work?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...