Varargs (Variable-Length Arguments) in Java
Varargs let a method accept any number of arguments of a given type — zero, one, or many — using a single parameter marked with three dots: static int sum(int... numbers). Inside the method body, that parameter behaves exactly like a regular int[] array, since that's genuinely what it is under the hood; the three-dot syntax is really just a convenience for the caller, who can pass individual comma-separated values instead of building an array explicitly first.
A method can have ordinary parameters before a varargs parameter, but the varargs parameter must come last, and a method can only have one — the compiler wouldn't otherwise know where one variable-length group ends and the next begins.
The syntax
Type... name in a parameter list accepts zero or more arguments of that type.
It's really an array underneath
Inside the method, the varargs parameter behaves exactly like an ordinary array of that type.
Calling with individual values
The caller can list arguments directly, comma-separated, without building an array first.
Calling with zero arguments
A varargs parameter can legally receive none at all, resulting in an empty array inside the method.
Placement rules
A varargs parameter must be the last one in the list, and a method can have at most one.
public class VarargsDemo { 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)); System.out.println(sum()); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.