What do the … dots in the method parameters mean?

Technology CommunityCategory: JavaWhat do the … dots in the method parameters mean?
VietMX Staff asked 3 years ago
Problem

What do the 3 dots in the following method mean?

public void myMethod(String... strings){
    // method body
}

That feature is called varargs, and it’s a feature introduced in Java 5. It means that function can receive multiple String arguments:

myMethod("foo", "bar");
myMethod("foo", "bar", "baz");
myMethod(new String[]{"foo", "var", "baz"}); // you can eve

Then, you can use the String var as an array:

public void myMethod(String... strings){
    for(String whatever : strings){
        // do what ever you want
    }

    // the code above is is equivalent to
    for( int i = 0; i < strings.length; i++){
        // classical for. In this case you use strings[i]
    }
}