What javac does
Varargs
The caller builds the array, even for no arguments.
A varargs parameter is an array parameter with the ACC_VARARGS flag. All the work happens at the call site: javac builds the array, even when there are no arguments.
The source
// Varargs: the caller builds the array; the method just takes one.
public class Varargs {
static int sum(int... values) {
int total = 0;
for (int v : values) total += v;
return total;
}
static int callers() {
return sum() + sum(1) + sum(1, 2, 3);
}
}Compiled by javac 25 with -g and --release 8 to 25. Where releases produce different bytecode, each version is shown.
What to look for
sum()creates an empty array:iconst_0,newarrayint.sum(1, 2, 3)isnewarrayfollowed by adup, an index, a value and aniastorefor each element.- Every call allocates a new array. That is why performance-minded APIs such as
List.ofalso have fixed-arity overloads for small counts.
Varargs.class
static int sum(int... values)
static int callers()
javac --release 8 to 25 (the same bytecode for every release)