6

What is the difference between String... args and String[] args in Java?

I'm new to Java programming. Can anyone plz tell me what's the difference between (String....args) & (String [] args) If I'll use first one in place of the second........Does it make any difference?

String... args will declare a method that expects a variable number of String arguments. The number of arguments can be anything at all: including zero.

String[] args and the equivalent String args[] will declare a method that expects exactly one argument: an array of strings.

One difference that might not spring out from those observations is that in the second case (but not the first) the caller may have a reference to the array. In both cases the method works with args as an array of Strings, but if it does things like swap the array elements this will not be visible to the caller if the String... args form is used.

Tiny
  • 27,221
  • 105
  • 339
  • 599
user3367304
  • 69
  • 1
  • 2
  • 1
    This is a good question, but it looks like it's been asked many times before. See http://stackoverflow.com/questions/301563/difference-fnstring-args-vs-fnstring-args – hungryghost Jun 20 '15 at 03:04

3 Answers3

14

If you have void x(String...args) method, you can call it as x("1", "2", "3") while void x(String[] args) can be called only as x(new String[] {"1", "2", "3"}).

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • This is the best answer. It clearly shows the difference in syntax/usage. – hungryghost Jun 20 '15 at 03:01
  • @hungryghost OP asked specifically about `main` (and even tagged the question [tag:main]). – Elliott Frisch Jun 20 '15 at 03:15
  • @ElliottFrisch, the question itself doesn't mention `main`, although I do see it is tagged that way. `String...args` vs `String[] args` really is more a general Java question, but if the OP is specifically wondering about main, your answer addresses that well. I'll upvote yours too (even if the whole question is a dup to begin with)! ;) – hungryghost Jun 20 '15 at 03:29
6

There is no difference. And String... args is allowed.

JLS-12.1.4. Invoke Test.main says (in part)

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)


public static void main(String... args)

Not explicitly listed (but also allowed)

public static void main(String args[])
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    I would just throw in that the last form, although technically allowed and valid, that the `String[]` form is generally preferable because the brackets indicate that the actual parameter type is `String[]` and the parameter name is a reference to an object of type `String[]`. More of a preference of convention I guess... – Ryan J Jun 20 '15 at 02:30
1

In the main method there is very little difference, they will both work the same way.

String... is known as varargs. The upside to varargs is it is very nice syntactic sugar which lets you indefinitely define arguments to a method call. In the method's body varargs is treated exactly as an array of the type.

The downside to varargs is Java will convert the indefinite arguments to a new array, thus creating garbage. As an alternative you can use method overloading to emulate varargs if garbage creation must be zero.

Jire
  • 9,680
  • 14
  • 52
  • 87