This question already has answers here:
What does "..." mean in Java? [duplicate]
How does the Java array argument declaration syntax "..." work?
(10 answers)
Closed 9 years ago.
What does the String... of the following function means?
public UpdateSettingsRequest(String... indices) {
this.indices = indices;
}
It is called varargs. It works for any type as long as it's the last argument in the signature.
Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.
A method that looks like:
void foo(int bar, Socket baz...)
will have an array of Socket (in this example) called baz.
So, if we call foo(32, sSock.accept(), new Socket()) we'll find an array with two Socket objects.
Calling it as foo(32, mySocketArray) will not work as the type is not configured to take an array. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...) can take multiple arrays of PrintStream and stick them into a single PrintStream[][].
Oddly enough, due to the fact that arrays are objects, Object... foo can take any number of arrays.
Related
This question already has answers here:
What is the ellipsis (...) for in this method signature?
(5 answers)
Closed 7 years ago.
I'm learning Java, and in function prototypes I often see parameters of the variety type... parameter_name. What does the ... notation mean?
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.
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).
This question already has answers here:
Java: Find out whether function was called with varargs or array
(2 answers)
Closed 8 years ago.
For instance, I have this method that uses var-args:
void doSomething(int... args);
And I can have 2 different usages:
// 1) using an array
int[] data = new int[] {1, 2};
doSomething(data);
// 2) using a sequence of arguments
doSomething(1, 2);
Assuming the method doSomething() needs to ensure the passed array is "immutable" from outside, it will copy the array. However, if the caller has passed a sequence of arguments, then there's no reference to the array from outside of the method, therefore we could avoid the cost an array copy.
Is it possible to determine how it was called in runtime?
No. As far as I know, it is not possible to determine which way the caller invoked the method, just that the method was invoked.
This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 9 years ago.
What do the three dots after "Object" mean in this parameter declaration:
public static int queryCount (
Connection conn, String whereClause,
Object ... params)
throws Exception
In what way does it differ from the parameter declaration Object params ?
Three dots mean that there method can get as parameters as much argument of type Object as it likes. Reading more about "varargs" arguments could be helpful.
This feature was introduced in Java to hide the process of using Arrays as parameters, in form of varargs.
As the documentation states, the process is stil same but complexity has been reduced.
Please note following points:
This allows for entering an array or sequence of type specified.
This form must be used at last in parameters list.
This is not available in older version, so be careful if you plan to deploy to older versions of Java
In short, it's a syntactic sugar for array with restriction that this should be the last parameter in arguments list.
e.g. it's totally legal to declare main method as follows
public static void main(String... args) {}
And another feature of this, this argument is optional, but you still will get an empty array as a value of argument.
This question already has answers here:
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 9 years ago.
I am currently trying to get AsyncTasks working in Android, and have found something I have never seen before over and over again in many different tutorials.
Certain methods in the tutorials are passed parameters that look like this String... arg0, Integer... values.
Here is a tutorial with some code shown similar to what I am describing.
What does this mean? Why is the ... there?
It is called varargs. It works for any type as long as it's the last argument in the signature.
Basically, any number of parameters are put into an array. This does not mean that it is equivalent to an array.
A method that looks like:
void foo(int bar, Socket baz...)
will have an array of Socket (in this example) called baz.
So, if we call foo(32, sSock.accept(), new Socket()) we'll find an array with two Socket objects.
Calling it as foo(32, mySocketArray) will not work. However, if the signature is a varargs of arrays you can pass one or more arrays and get a two-dimensional array. For example, void bar(int bar, PrintStream[] baz...) can take multiple arrays of PrintStream and stick them into a single PrintStream[][].
This question already has answers here:
Can I pass an array as arguments to a method with variable arguments in Java?
(5 answers)
What do 3 dots next to a parameter type mean in Java?
(9 answers)
Closed 9 years ago.
I am wondering how the parameter of ... works in Java. For example:
public void method1(boolean... arguments)
{
//...
}
Is this like an array? How I should access the parameter?
Its called Variable arguments or in short var-args, introduced in Java 1.5.
The advantage is you can pass any number of arguments while calling the method.
For instance:
public void method1(boolean... arguments) throws Exception {
for(boolean b: arguments){ // iterate over the var-args to get the arguments.
System.out.println(b);
}
}
The above method can accept all the below method calls.
method1(true);
method1(true, false);
method1(true, false, false);
As per other answer, it's a "varargs" parameter. Which is an array.
What many people don't realise is two important points:
you may call the method with no parameters: method1();
when you do, the parameter is an empty array
Many people assume it will be null if you specify no parameters, but null checking is unnecessary.
You can force a null to be passed by calling it like this:
method1((boolean[])null);
But I say if someone does this, let it explode.