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

JavaArraysDeclaration

Java Problem Overview


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.

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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

Solution 4 - Java

Solution 5 - Java

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.

Solution 6 - Java

You might want to read up on Using Variable Arguments (or varargs) in Java.

Solution 7 - Java

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

Solution 8 - Java

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

Solution 9 - Java

Variable arguments. Can have 0 or more String arguments.

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

Solution 10 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVirtually RealView Question on Stackoverflow
Solution 1 - JavaJohnSView Answer on Stackoverflow
Solution 2 - JavahvgotcodesView Answer on Stackoverflow
Solution 3 - JavajmjView Answer on Stackoverflow
Solution 4 - Javaicyrock.comView Answer on Stackoverflow
Solution 5 - JavacthulhuView Answer on Stackoverflow
Solution 6 - JavaJustin NiessnerView Answer on Stackoverflow
Solution 7 - JavaMing-TangView Answer on Stackoverflow
Solution 8 - JavaBrian ToppingView Answer on Stackoverflow
Solution 9 - JavaJan ZykaView Answer on Stackoverflow
Solution 10 - Javadj18View Answer on Stackoverflow