Varargs is a syntax that allows a method to receive a variable number of arguments of a single type. They are declared using ellipsis:
public void printNames(String... names) { for (String name : names) { System.out.println(name); } }
The varargs parameter inside a method is simply an array of the corresponding type: String[] names in the example above. You can call the method with any number of arguments or with an array:
printNames("Igor", "Anna", "Maria"); printNames(new String[] {"Alex", "Inna"});
Nuances of usage:
Question: What will be the length of the varargs parameter array if you call the method without passing any arguments?
Answer: In this case, the varargs variable will refer to an array of zero length: new String[0]. It will not be null!
public void foo(Integer... args) { System.out.println(args.length); } // foo(); // Prints: 0
Story
In the logging library, there was a method log(String... messages). When calling log(null), a NullPointerException occurred inside the method (expected messages to be an empty array, instead it was null). To resolve this, a null check had to be added.
Story
A developer created an overload:
public void doSomething(String[] arr) {...} public void doSomething(String... arr) {...}When passing an array, a different method than expected was called, resulting in data loss. The difference between an array and varargs turned out to be non-obvious.
Story
In the project, a second parameter was added after varargs:
public void foo(String... args, int count) {...} // Compilation error!This led to an error and build failure, as varargs must be the last parameter.