3

So I've been trying to figure out the point of String... in Java. From what I understand, it's about the same as String[], but I can't find any information about it on here or on Google.

Can someone tell me what it is and the proper usage for something like that? (Lets say I have a method with the parameters myMethod(String... args) { ... }

Thanks!

Kyle Colantonio
  • 454
  • 1
  • 3
  • 21

3 Answers3

7

This syntax is indeed identical to the String[] as far as the type is concerned. However, it allows callers to construct an array implicitly by specifying a variable number of String parameters:

myMethod("a", "b", "c");
myMethod(); // Empty array is also allowed
myMethod("a", "b");

One limitation on this syntax is that arguments like that may appear only at the end of the method's parameter list.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Would that mean it can be converted to a String[] object? – Kyle Colantonio Aug 25 '13 at 20:08
  • @IAreKyleW00t. It is converted to `String[]`. – Rohit Jain Aug 25 '13 at 20:10
  • 2
    @IAreKyleW00t Absolutely: this is a compile-time feature, at runtime the class is identical to one with the `myMethod(String[])` and the calls would be identical to `myMethod(new String[] {"a", "b", "c"})`. – Sergey Kalinichenko Aug 25 '13 at 20:10
  • 1
    You can also call it with an explicit array: `myMethod(new String[] {"a", "b", "c"});` The compiler matches it up nicely. Using an explicit empty array is sometimes the only way to get the compiler to call the varargs version of a method when there's another overloaded method that is just missing the last formal parameter, or to resolve otherwise ambiguous method overloads (different type for last varargs parameter). – Ted Hopp Aug 25 '13 at 20:24
2

It means that myMethod accepts zero or more String arguments. See here, for instance. In the Java Language Specification, this is called a variable arity parameter:

The last formal parameter of a method or constructor is special: it may be a variable arity parameter, indicated by an ellipsis following the type.

If the last formal parameter is a variable arity parameter, the method is a variable arity method. Otherwise, it is a fixed arity method.

The JLS also describes in gruesome detail how method signatures are matched when there is a variable arity parameter.

Community
  • 1
  • 1
Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
2

The ... signify that the method supports var-args i.e. variable (number of) arguments.

So, you could call your myMethod() as

myMethod(); // or
myMethod(str1); // or
myMethod(str1, str2); // and so on

using the same method signature myMethod(String... str). When declaring a var-arg method with multiple parameters the var-arg parameter must succeed after all other normal method parameters.

Inside the method all the passed arguments become accessible as an array with the same reference name as that of the parameter. So, you can actually use str.length here.

The feature is quite flexible. You can even pass a String[] to it. But, the reverse is not true. A method taking an array would not accept individual strings.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89