Take the 2-minute tour ×

Possible Duplicate:
Can I pass an array as arguments to a method with variable arguments in Java?
What is … in a method signature

I first saw this when I was modding Minecraft. It had a constructor that specified (String ... line), and thought it was just some shorthand that Mojang had created. But now, I was looking over ProcessBuider, and saw it again. I was wondering what this is used for. My best guess is that it allows developers to add as many of that type of object as they want. But if that's the case, why not just use an Array or List?

So, really, I am asking two questions:

  1. What is the "..." operator, and
  2. Why would it be more useful than using an Array or List?
share|improve this question

marked as duplicate by CoolBeans, Jon Lin, Daniel DiPaolo, Lukas Knuth, Maerlyn Aug 14 '12 at 11:35


It's called varargs. Here's more info: stackoverflow.com/questions/766559/… –  Mike Clark Aug 13 '12 at 22:55
It's called "varargs". There, you just got a new search keyword :) –  BalusC Aug 13 '12 at 22:55
Sorry about the possible dupe, I had no idea what it was called. –  thatJavaNerd Aug 13 '12 at 22:57
Upvotes? Is this really an interesting question or a new concept? –  Joel Aug 13 '12 at 22:58
@Joel Thank you :) –  thatJavaNerd Aug 13 '12 at 22:59

5 Answers 5

up vote 2 down vote accepted

... indicates a multiple argument list to a variadic function: a function that can take a variable number of arguments.

For an example of this, look at PrintStream.format. The first (required) argument is a format String, and the remaining 0 or more arguments fulfill that format.

share|improve this answer
Nice answer! Very detailed, succinct, and gave an example. +1 –  thatJavaNerd Aug 13 '12 at 23:07

It is called varargs, and as you say it is used to be able to let a method be called with any number of arguments of the specified type. It was introduced in Java 5.
You can read more in the Java tutorials - Varargs.

share|improve this answer
That's really useful! Thanks for the link, too. –  thatJavaNerd Aug 13 '12 at 23:00

This is equivalent to a String[] line. It is Java's equivalent to the varargs keyword in C/C++. Similar to C/C++ it must appear as the last parameter.

share|improve this answer
I didn't know that :) Thanks! –  thatJavaNerd Aug 13 '12 at 23:01

You've already answered question #1 yourself. As to why it's more useful, it's just a shorthand that requires less typing.

share|improve this answer
I didn't even notice that :) –  thatJavaNerd Aug 13 '12 at 22:59
I wouldn't say it's short hand. I would say it's convince as it allows you to pass 0/1 or more parameters without the need to supply multiple method signatures –  MadProgrammer Aug 13 '12 at 22:59

To answer your second question, one advantage of varargs is that you can call a function taking varargs parameter without passing that param. Whereas instead if your function takes in an array, and you need to call it without any value, the caller needs to explicitly pass null.

share|improve this answer
