What do the 3 dots following String
in the following method mean?
public void myMethod(String... strings){
// method body
}
This is the Java way to pass varargs (variable number arguments).
If you are familiar with C, this is similar to the ...
syntax used it the printf
function:
int printf(const char * format, ...);
but in a type safe fashion: every argument has to comply with the specified type (in your sample, they should be all String
).
This is a simple sample of how you can use varargs:
class VarargSample {
public static void PrintMultipleStrings(String... strings) {
for( String s : strings ) {
System.out.println(s);
}
}
public static void main(String[] args) {
PrintMultipleStrings("Hello", "world");
}
}
The ...
argument is actually an array, so you could pass a String[]
as the parameter.