varargs and the ‘…’ argument

From the docs on varargs:

The three periods after the final
parameter’s type indicate that the
final argument may be passed as an
array or as a sequence of arguments.

So you can pass multiple arguments or an array.

The following works just fine:

class VarargTest {
  public static void main(String[] args) {
    Object[] params = {"x", 1.2345f};
    String s = String.format("%s is %.2f", params);
    System.out.println(s); // Output is: x is 1.23
  }
}

Leave a Comment