29

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

Jim
  • 1,966
  • 3
  • 24
  • 33
  • 2
    Possible duplicate of [Java, 3 dots in parameters](http://stackoverflow.com/questions/3158730/java-3-dots-in-parameters) – Raedwald Dec 04 '15 at 12:58

8 Answers8

19

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
15

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.

ewein
  • 2,695
  • 6
  • 36
  • 54
Margus
  • 19,694
  • 14
  • 55
  • 103
  • Thanks all. I totally understand. I guess not knowing this I would have just used a String[] in its place. But now I can read the code others write. – Jim Oct 18 '10 at 15:48
  • "You can't even simply make generic array of elements" Then you will get a warning when using varargs in this way, because it will also try to create a generic array behind the scenes, but can't – newacct Jun 26 '12 at 17:42
  • I should mention, that **new MBTN(Arrays.asList("Red", "Blue", "Green"));** would be considered better. – Margus Mar 16 '16 at 11:28
14

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.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
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");
Mike Deck
  • 18,045
  • 16
  • 68
  • 92
1

They are variable length parameters.

Here is one link with an example.

Andy
  • 8,841
  • 8
  • 45
  • 68
1

As mentioned by everyone...variable arguments (or varargs) allows you to do this....

//Method we're using for varargs
public void doSomething(String... datas) {
    if (datas == null || datas.length == 0) {
        System.out.println("We got nothing");
    } else {
        for (String data: datas) {
            System.out.println(data);
        }
    }
}

Therefore, all these calls mentioned below are valid....

String d[] = {"1", "2", "3"};
doSomething(d); //An array of String, as long as the type is exactly as the varargs type.

//OR
doSomething("1", "2", "3", "4"); //I can add "infinitely" many arguments as the JVM can allocate me....

//OR 
doSomething("1"); //Exactly 1;

Internally varargs is "essentially" a reference array of it's declared type.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

It's a java method that took an arbitrary variable number of parameters

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
0

It means "Arbitrary number of arguments" meaning you can pass a unknown number of parameters into the method..

see

http://download.oracle.com/javase/tutorial/java/javaOO/arguments.html Look for the "Arbitrary number of arguments" section

public Polygon polygonFrom(Point... corners) {
    int numberOfSides = corners.length;
    double squareOfSide1, lengthOfSide1;
    squareOfSide1 = (corners[1].x - corners[0].x)*(corners[1].x - corners[0].x) 
            + (corners[1].y - corners[0].y)*(corners[1].y - corners[0].y) ;
    lengthOfSide1 = Math.sqrt(squareOfSide1);
    // more method body code follows that creates 
    // and returns a polygon connecting the Points
}
Mads Lee Jensen
  • 4,570
  • 5
  • 36
  • 53