I'm learning Java, and in function prototypes I often see parameters of the variety type... parameter_name
. What does the ...
notation mean?

- 2,903
- 5
- 33
- 52
2 Answers
The New Features and Enhancements J2SE 5.0 says (in part)
Varargs
This facility eliminates the need for manually boxing up argument lists into an array when invoking methods that accept variable-length argument lists. Refer to JSR 201.
It's also called a variadic function. Per the wikipedia,
In computer programming, a variadic function is a function of indefinite arity, i.e., one which accepts a variable number of arguments.

- 198,278
- 20
- 158
- 249
Those are called varargs. When calling the method you can pass in any number of arguments for that parameter (even 0, so you can ignore it), or you can pass in an array. Inside the method the varags parameter is treated as an array.
For example, this method:
public void foo(String... strs) {}
Can be called with any of these:
foo();
foo("hello", "world");
String[] args = {"hello", "world"};
foo(args);
Inside the method you can access parameters like so:
String str1 = strs[0];
String str2 = strs[1];
One important thing to note is that a varargs parameter must be the last parameter in your method (It makes sense when you consider that the number of arguments passed can vary, so you have to resolve the other parameters first).

- 5,537
- 4
- 30
- 48