6

Possible Duplicate:
What is the ellipsis (…) for in this method signature?
java: how can i create a function that supports any number of parameters?

well i'm trying to figure out some examples and I found this kind of array definition for arguments in the main method. What's so special about this "..." and what's the difference between a normal String[] args?

Thanks

Community
  • 1
  • 1
Arturas M
  • 4,120
  • 18
  • 50
  • 80
  • 3
    You probably mean varargs: http://stackoverflow.com/questions/4215698/java-how-can-i-create-a-function-that-supports-any-number-of-parameters – wkl May 03 '12 at 01:24
  • See this [**post**](http://stackoverflow.com/q/2367398/844882) for more info on how elipsis work in Java. – Alex Lockwood May 03 '12 at 01:26
  • @Adam, how on earth is "what have you tried" relevant to a question about the meaning of a specific piece of syntax? (i mean, i think this question should be closed as a dup, but "what have you tried" seems totally irrelevant) – Kirk Woll May 03 '12 at 01:32
  • @KirkWoll Follow the link and read the section starting with "Try taking a few minutes to run through these points:" The site does far more than just tell you to "try it yourself"; it gives some very good advice for asking effective questions. – Adam Liss May 03 '12 at 01:35
  • 1
    @Adam, I see your point, but in that case, the domain name is particularly unhelpful. Better would be along the lines of Jon Skeet's ["Writing the perfect question"](http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx). No ambiguity and no confusion. But I concede this isn't your problem. :) – Kirk Woll May 03 '12 at 01:37
  • Thanks for the link -- it'll come in handy. – Adam Liss May 03 '12 at 01:57
  • http://stackoverflow.com/questions/8756948/using-ellipsis-in-main-method – Josh Lee Jun 05 '12 at 13:26

4 Answers4

7

That's a notation from Java 5 for variable length argument lists. It is roughly equivalent to a String array, but lets you pass individual parameters that are combined into an array automatically.

void mymethod(String... a) {
    for (String s : a) {
        ...
    }
}

mymethod("quick", "brown", "fox");

This makes sense only when you plan to call a method from your program, so I do not see why it would be desirable to use this syntax for your main(). It will work, however.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3

The elipsis (...) are varargs. They are used when you want to create a method with any number of arguments. Oracle explains how varargs work in detail here.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
2

This is called varargs which means any number of arguments can be passed of that type (String).

1.it should be in final position

2.It will be processed as an array

Balaswamy Vaddeman
  • 8,360
  • 3
  • 30
  • 40
1

... is used for varargs.

For example

public void myMethod(Object... params) { }

The params variable is optional and is treated as a nullable array of Objects.

Sunil
  • 1,238
  • 3
  • 13
  • 21