Varargs
Varargs let a method accept zero or more arguments of one type. You declare the last parameter as Type... name. The compiler packs the arguments into an array.
Detailed Explanation
String.format and List.of take a flexible number of values. The mechanism is varargs: Object... args. Inside the method, args is an array. Callers write of("a", "b", "c") instead of allocating the array themselves. They can still pass an array explicitly.
Only the last parameter may be varargs. You cannot have two varargs lists. Overload resolution prefers a non-varargs method when both match, which is why List.of() and List.of(E) exist as separate overloads beside List.of(E...). Those extra overloads also avoid the heap array for the common small cases.
A call with zero arguments produces an empty array, not null. A call that passes null as the only argument, with no cast, is ambiguous or becomes a null array reference: args itself is null, and args.length throws. Write method((String) null) or method(new String[] { null }) when you mean a single null element.
Generics and varargs interact badly. A generic varargs parameter (T...) may allocate an array of a type that has been erased, which produces a heap-pollution warning. The JDK marks such methods @SafeVarargs when they only read the array. If you store into a generic varargs array, you can create heap pollution that fails later with ClassCastException.
You reach for varargs when the method is a builder of a list, a formatter, or a helper that would otherwise force callers to write new Type[] { ... } every time. You avoid them when the values have different roles (start, end, step should be three parameters) or when you need two lists of variable length.
The cost is a hidden array allocation on every call, and a fuzzier signature. A method that takes Object... will accept anything, including mistakes. Prefer String... or a typed list. For public APIs, offer overloads for zero, one, and two arguments if those calls are hot.
Code Examples
public class Join {
static String join(String sep, String... parts) {
if (parts.length == 0) {
return "";
}
StringBuilder sb = new StringBuilder(parts[0]);
for (int i = 1; i < parts.length; i++) {
sb.append(sep).append(parts[i]);
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(join(",")); // prints: (empty)
System.out.println(join(",", "a")); // prints: a
System.out.println(join(",", "a", "b", "c")); // prints: a,b,c
System.out.println(join("-", new String[] {"x", "y"})); // prints: x-y
}
}import java.util.List;
public class Of {
public static void main(String[] args) {
List<String> names = List.of("ana", "bo"); // varargs overload
System.out.println(names.size()); // prints: 2
}
}Common Gotchas
`foo(null)` can make the array itself null
`parts.length` then throws `NullPointerException`. Pass `new String[] { null }` for one null element.
Generic `T...` can pollute the heap
do not write into the array. Add `@SafeVarargs` only on `static` or `final` methods that only read it.
A non-varargs overload wins
`log(String msg)` is chosen over `log(String... msgs)` for a single string.
Varargs must be the last parameter
`void f(int... xs, String name)` does not compile.