What does the "..." mean in a parameter list? doInBackground(String... params)

33,436

Solution 1

This is called Variadic function (wiki page with examples in many languges).

In computer programming, a variadic function is a function of indefinite arity, i.e. one which accepts a variable number of arguments. Support for variadic functions differs widely among programming languages. There are many mathematical and logical operations that come across naturally as variadic functions. For instance, the summing of numbers or the concatenation of strings or other sequences are operations that can logically apply to any number of operands. Another operation that has been implemented as a variadic function in many languages is output formatting. The C function printf and the Common Lisp function format are two such examples. Both take one argument that specifies the formatting of the output, and any number of arguments that provide the values to be formatted. Variadic functions can expose type-safety problems in some languages. For instance, C's printf, if used incautiously, can give rise to a class of security holes known as format string attacks. The attack is possible because the language support for variadic functions is not type-safe; it permits the function to attempt to pop more arguments off the stack than were placed there -- corrupting the stack and leading to unexpected behavior. Variadic functionality can be considered complementary to the apply function, which takes a function and a list/sequence/array as arguments and then calls the function once, with the arguments being the elements of the list.

One of may personal favorite not used features in Java. It is basically a reference array that is built from elements. One of the best ways to use it is on class constructor, or method where you need to constantly find a value like maximum of 2, 3, 4, 5 input elements.

One example is, when i built a generic binary tree node, for coding tasks, I used this in constructor. This enabled me simply add elements to the tree and distribute them.

Following creates String type binary tree, with root "Red" and 2 branches "Blue" and "Green".

new MBTN<String>("Red", "Blue", "Green").

Could you think what the alternatives would be :D You can't even simply make generic array of elements, so this would stretch like hell. It is definitely not useless.

Solution 2

It's called varargs. This fact should yield better Google results.

Solution 3

They are the "variable arguments" or varargs (for short).

Basically it allows the passing of an unspecified number of Strings, so the method signature

public void printStuff(String...messages)

Effectively can handle the following calls

printStuff("hi");
printStuff("hi", "bye");
printStuff("Hello", "How are you?", "I'm doing fine.", "See you later");

You can effectively consider this a type of autoboxing. The printStuff argument can be seen as an array, so printStuff(String...messages) is conceptually handled like printStuff(String[] messages). Wtih the calls above effectively acting like

printStuff(new String[] {"hi"});
printStuff(new String[] {"hi", "bye"});
printStuff(new String[] {"Hello", "How are you?", "I'm doing fine.", "See you later"});

To access the messages internally, you use typical List handling primitives. Something like

...
if (messages != null) {
  for (String message : messages) {
    System.out.println(message);
  }
}
...

That there is no need to actually create arrays is a bit of syntactic sugar added to Java with the advent of auto boxing.

Solution 4

As @BalusC mentioned, it's a varags parameter. This means you can pass a variable number of arguments to that method.

So for a method defined as

public void foo(String...strings) {  }

The following invocations are legal:

foo();
foo("one param");
foo("one", "two", "three");

Solution 5

They are variable length parameters.

Here is one link with an example.

Share:
33,436
Jim
Author by

Jim

Past 3 years developing / coding Internet facing API's in node, typescript.

Updated on July 22, 2022

Comments

  • Jim
    Jim almost 2 years

    I don't understand that syntax. Trying to google various words plus "..." is useless.