Varargs in Java (variable-arity parameters) let a method accept zero or more arguments of the same type, written as Type... name in the parameter list. Inside the method, the parameter is an ordinary array, and the compiler builds that array from the arguments at each call site.
This guide explains the syntax, the rules that govern where a varargs parameter can go, how overload resolution treats it, and the pitfalls that catch intermediate developers: generic varargs warnings, null arguments and hidden array allocation. Examples target Java 21 and Java 25.
Put three dots after the parameter type. The method can then be called with any number of arguments of that type, including none.
Inside sum, numbers has the type int[]. With no arguments it is an empty array, never null, so the loop above is safe. The standard library uses the same feature in String.format(String, Object...), Arrays.asList(T...), List.of(E...) and Path.of(String, String...).
The compiler enforces three rules.
Putting required parameters first is a good design habit. A method like max(int... values) accepts zero arguments, which has no sensible maximum. Write max(int first, int... rest) so the caller must supply at least one value.
Because varargs is an array underneath, a caller can pass an existing array directly. Java uses it as the argument array without copying.
Since the array is shared, a method that modifies its varargs parameter modifies the caller's array. When you pass separate values, the array is created fresh, so the change is invisible. If your method must not alter the input, copy it first with Arrays.copyOf or avoid writing to it.
Java resolves overloaded calls in phases. First it looks for a method applicable without boxing and without varargs, then with boxing, and only then does it consider varargs methods. A varargs overload is therefore the last resort.
Ambiguity appears when two varargs overloads both fit, for example f(int... a) and f(long... a) called as f(). The compiler reports "reference to f is ambiguous". Avoid overloading a varargs method with another varargs method that differs only in element type.
Two calls look similar but behave differently when the element type is a reference type.
A bare count(null) compiles with a warning because null can match Object[], so the array itself becomes null. Cast to make your intent explicit, and guard against a null array in public APIs that accept reference types.
Passing a String[] to an Object... parameter is also special: the array is used as the argument array, so each string becomes one element rather than the whole array being one element. Pass (Object) strings if you want a single argument.
A generic varargs parameter such as List<String>... lists creates a generic array, which the language cannot do safely. The compiler warns about possible heap pollution.
@SafeVarargs tells the compiler you promise the method only reads from the array and does not let it escape. It is allowed on static methods, final methods, private methods and constructors. Do not use it if you store the array in a field or return it.
Each call that passes separate arguments allocates a new array. That cost is tiny for most code, but it matters in a hot loop. Overloads with fixed parameters avoid the allocation, which is why List.of has dedicated overloads for one to ten elements before its varargs version.
Use varargs when callers genuinely pass a small, variable number of similar values. When the data already lives in a collection, accept a List<T> or Collection<T> instead. Never use varargs for values of different meaning, because the compiler cannot check which position means what.
No. A method can declare only one varargs parameter, and it must be the last parameter.
No. It is an empty array. It is null only if the caller explicitly passes a null array.
int... and int[]?Inside the method they are identical. The difference is at the call site: int... lets callers pass separate values, while int[] requires an array.
Yes. public static void main(String... args) is valid and equivalent to String[] args. Java 25 also allows simpler instance main methods in compact source files, but the argument is still an array of strings.
Varargs (Type... name) lets a method take zero or more arguments of one type and exposes them as an array. It must be the last parameter, and only one is allowed. Overload resolution picks fixed-arity methods first. Watch for the null array case, use @SafeVarargs only on methods that do not leak the array, and prefer a collection or fixed overloads when allocation or clarity matters.