CKTIS / Java
Java varargs: String... and T...
In Java SE 21, a varargs parameter accepts separate arguments or an existing array. With `T...`, type erasure also introduces a risk of heap pollution.
Version: Java SE 21
Syntax
static void labels(String... values) { /* values has type String[] */ }
static <T> int count(T... values) { return values.length; }In Java SE 21, ... after the type marks a varargs parameter. A method or constructor can have at most one, and it must come last. Inside the method, String... values has type String[]; in a generic method with type parameter <T>, T... values has type T[].
When a call uses variable arity, the compiler arranges for the trailing arguments to be packed into a new array. labels() receives an empty array, not null. If the call fits without packing, a compatible array is passed without copying; changes to its elements are visible to the caller. For the method shown, labels(null) and labels((String[]) null) pass a null array reference; reading its length would throw NullPointerException. labels((String) null) passes an array with one null element.
Overload resolution first looks for a match without packing, first without boxing or unboxing and then with them allowed. Only if neither phase finds an applicable method does it consider packing. A varargs declaration can therefore be selected in a fixed-arity phase. labels(String...) and labels(String[]) have the same signature and cannot be separate overloads.
String is reifiable: its type is fully available at run time. T is not reifiable; when packing is needed, the new array uses the erasure of the array type determined for that call. The compiler issues an unchecked warning at the declaration and may also warn at a call site. If the array is aliased as Object[], a store can break generic type guarantees. This heap pollution may show up later as a ClassCastException when reading a value.
@SafeVarargs is the author's assertion, not a run-time check. It suppresses these warnings at the declaration and at call sites. It is allowed on varargs constructors and on static, final, or private varargs methods. Use it only after checking that the implementation neither uses nor exposes the array in an unsafe way.
Examples
import java.util.Arrays;
import java.util.List;
public class VarargsDemo {
static void labels(String... values) {
System.out.println(Arrays.toString(values));
}
@SafeVarargs
static <T> int count(T... values) {
return values.length;
}
public static void main(String[] args) {
labels();
labels("red", "blue");
labels(new String[]{"green"});
labels((String) null);
labels((String[]) null);
System.out.println(count(List.of("red", "blue"), List.of("green")));
}
}Result
[] [red, blue] [green] [null] null 2
`[]` is an empty array, `[null]` is an array with one null element, and `null` means no array. `count` reads only the array length: it counts two lists, not three strings.
Related lessons
- Java · Lesson 3 · Advanced · Version: 2
- Java · Lesson 14 · Advanced · Version: 2
Sources
- Java Language Specification, Java SE 21 — §8.4.1 Formal Parameters
- Java Language Specification, Java SE 21 — §8.4.2 Method Signature
- Java Language Specification, Java SE 21 — §15.12.2 Compile-Time Step 2: Determine Method Signature
- Java Language Specification, Java SE 21 — §15.12.4.2 Evaluate Arguments
- Java Language Specification, Java SE 21 — §4.7 Reifiable Types
- Java Language Specification, Java SE 21 — §4.12.2 Variables of Reference Type
- Java SE 21 API — SafeVarargs
- Java SE 21 API — Arrays.toString(Object[])
- JDK 21 Tool Specifications — The javac Command