Alex Rivera | Logout

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

Asked 2010-10-18T14:38:35.003
31

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

Edit
Report

3 Answers

16

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.

answered 2010-10-18T14:57:36.737
1

They are variable length parameters.

Here is one link with an example.

answered 2010-10-18T14:40:26.657
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
}
answered 2010-10-18T14:42:23.170

Your Answer