Varargs: Variable-Length Arguments
Java can accept arguments without mentioning number of arguments. This feature is called varargs and it is
short for variable-length arguments. A method that takes a variable number of arguments is called a variable-arity method, or simply a varargs method.
public class VariableLengthArguments { static void vaTest(int... v) { // variable length arguments,no need to // define // individual arguments to match passing // arguments System.out.print("Number of args: " + v.length + " Contents: "); for (int x : v) System.out.print(x + " "); System.out.println(); } public static void main(String args[]) { int n1[] = { 101, 102, 103, 104, 105 }; int n2[] = { 1, 2, 3 }; int n3[] = {}; vaTest(n1); // 5 arguments vaTest(n2); // 3 args vaTest(n3); // no args } }