Java Variable Arguments (varargs)
In this page:
Introduction to Varargs
Varargs (variable arguments) let a method accept any number of arguments of the same type without the caller needing to build an array first, using the Type... name syntax. Internally, Java collects whatever arguments you pass into a regular array before the method body runs.
Example: Introduction to Varargs
public class Main {
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum(1, 2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Varargs Syntax Rules
A method can declare at most one varargs parameter, and it must be the last parameter in the signature, because the compiler needs to know unambiguously where the fixed parameters end and the variable-length group begins.
Example: Varargs Syntax Rules
public class Main {
static void printAll(String label, int... numbers) {
System.out.print(label + ": ");
for (int n : numbers) {
System.out.print(n + " ");
}
}
public static void main(String[] args) {
printAll("Scores", 90, 85, 77);
}
}
Login to try C/C++/Java/PHP code in the editor
Varargs as Arrays
Since the varargs parameter really is just an array under the hood, you can pass an existing array directly instead of listing individual values, and the method behaves identically either way. This is handy when the values already come from another array or a collection converted to one.
Example: Varargs as Arrays
public class Main {
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
public static void main(String[] args) {
int[] values = {4, 5, 6};
System.out.println(sum(values));
}
}
Login to try C/C++/Java/PHP code in the editor
Overloading Varargs Methods
You can overload a varargs method with fixed-arity versions of the same name, but the compiler prefers the most specific matching signature, so ambiguous overloads (e.g. two versions that could both match a given call) will fail to compile rather than silently picking one.
Example: Overloading Varargs Methods
public class Main {
static int sum(int a, int b) {
return a + b;
}
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum(1, 2)); // matches the fixed-arity version
System.out.println(sum(1, 2, 3));
}
}
Login to try C/C++/Java/PHP code in the editor
Empty Varargs
Calling a varargs method with no arguments at all is legal; the parameter still exists inside the method as an array, just with a length of 0. Code that assumes the array always has at least one element will throw an ArrayIndexOutOfBoundsException in that case, so guard against an empty array explicitly.
Example: Empty Varargs
public class Main {
static int sum(int... numbers) {
System.out.println("Length: " + numbers.length);
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
public static void main(String[] args) {
System.out.println(sum());
}
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: