Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

People also ask

What is the syntax of array declaration in Java?

The syntax for declaring an array is: datatype[] arrayName; datatype : The type of Objects that will be stored in the array eg. int , char etc.

How do you declare an array array in Java?

Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets: String[] cars; We have now declared a variable that holds an array of strings.

How do you input an array as an argument in Java?

To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type. Given below is the method prototype: void method_name (int [] array);

What is the correct way of array declaration?

Array declaration syntax is very simple. The syntax is the same as for a normal variable declaration except the variable name should be followed by subscripts to specify the size of each dimension of the array. The general form for an array declaration would be: VariableType varName[dim1, dim2, ...


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


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

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

  • var-arg-of-object-arrays-vs-object-array-trying-to-understand-a-scjp-self-test

This is called variadic arguments, check here:

  • http://en.wikipedia.org/wiki/Variadic_function#Variadic_functions_in_C.23.2C_C.2B.2B.2FCLI.2C_VB.net.2C_and_Java

The official documentation (Java 1.5) is here:

  • http://download.oracle.com/javase/1.5.0/docs/guide/language/varargs.html

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.