4 回答
...
is a variable argument list.
Inside the function it may be treated as an array.
The ...
in general stands for variable number of arguments
So you can have a method with variable number of arguments of any type.
The strange symbol you're seeing in your output has nothing to do with this. You may try to see what's inside with Arrays.toString() method:
$ cat A.java
import java.util.Arrays;
public class A {
public void example( String ... args ) {
System.out.println( Arrays.toString( args ) );
}
public static void main( String ... args ) {
A a = new A();
a.example("ping", "pong\n");
a.example("That's", "all", "folks");
}
}
$ java A
[ping, pong
]
[That's, all, folks]
When you call the function, it turns the list of comma separated arguments into an array of strings.
String...
is indeed a syntaxic sugar that replaces String[]
and allows you to pass a undefined numbers of arguments to a method.
Example :
public void withArray(String[] values){ ... }
withArray(new String[] {"a", "b"});
public void withVarArgs(String... values) { ... }
withVarArgs("a", "b");
Variable args allow you to pass arguments directly, skipping new String[]
declaration.