By WebNest Studio Engineering Team
Java - Core Tutorial
Java Varargs
Varargs (variable-length arguments) let a method accept zero, one, or many arguments of the same type without the caller having to build an array explicitly. The syntax uses three dots after the type: Type... name, and inside the method body, that parameter behaves exactly like an array of that type.
Why it matters
Varargs lets a method accept any number of arguments without the caller building an array, which is how String.format and List.of work. It is convenient, but it has rules — one per method, last in the list — and it is the last choice in overload resolution, which can make a call pick a different method from the one you expected.
Varargs Syntax and Rules
A varargs parameter is declared as returnType methodName(Type... name). Two rules are strict and enforced by the compiler.
- A method can have at most one varargs parameter.
- The varargs parameter must be the last parameter in the method signature.
- Inside the method, the varargs parameter is treated as an ordinary array (
Type[]), so you can use.length, indexing, and enhanced for-loops on it. - Callers may pass individual comma-separated values, an existing array of that type, or nothing at all (an empty array is created automatically).
Overload Resolution with Varargs
When a call could match either a fixed-arity overload or a varargs overload, Java always prefers the fixed-arity (non-varargs) method if one matches exactly, and only falls back to the varargs version when no better match exists. This avoids unnecessary array creation whenever a simpler overload is available.
Example
A varargs method summing any number of int arguments, plus passing an array directly
public class VarargsDemo {
static int sum(String label, int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
System.out.println(label + ": " + numbers.length + " values, total " + total);
return total;
}
public static void main(String[] args) {
sum("Call 1"); // zero arguments
sum("Call 2", 5); // one argument
sum("Call 3", 1, 2, 3, 4); // multiple arguments
int[] existing = {10, 20, 30};
sum("Call 4", existing); // passing an existing array directly
}
}
Call 1: 0 values, total 0
Call 2: 1 values, total 5
Call 3: 4 values, total 10
Call 4: 3 values, total 60
Common Mistakes
- Trying to declare a varargs parameter anywhere other than last in the parameter list — the compiler rejects it.
- Attempting to declare two varargs parameters in the same method, which is not allowed.
- Forgetting that a varargs parameter is really an array internally, and misusing it as if it were a single value inside the method.
- Creating ambiguous overloads between a varargs method and a fixed-arity method that could both match the same call.
Exercise
Write average(int... values) so that it returns the average of its arguments as a double, and returns 0.0 when called with no arguments instead of dividing by zero.
Expected output: 4.0 then 0.0
public class Average {
static double average(int... values) {
// TODO: return 0.0 when there are no values
// TODO: otherwise return the sum divided by the count, as a double
return 0;
}
public static void main(String[] args) {
System.out.println(average(2, 4, 6));
System.out.println(average());
}
}
Show hints
- Inside the method,
valuesis an ordinaryint[], sovalues.lengthtells you how many were passed. - Cast the sum to double before dividing so the result is not truncated.
Show solution
public class Average {
static double average(int... values) {
if (values.length == 0) {
return 0.0;
}
int sum = 0;
for (int value : values) {
sum += value;
}
return (double) sum / values.length;
}
public static void main(String[] args) {
System.out.println(average(2, 4, 6)); // 4.0
System.out.println(average()); // 0.0
}
}
Quick quiz
1. Where must a varargs parameter appear in a method's parameter list?
- A. First
- B. It must be the only parameter
- C. Anywhere
- D. Last
Show answer
D. The varargs parameter collects all remaining arguments, so it has to come last.
2. How many varargs parameters can one method have?
- A. One
- B. Two
- C. Any number
- D. One per type
Show answer
A. Only one, since the compiler could not tell where one variable-length list ends and the next begins.
3. A method sum(int... nums) is called as sum(). What is nums inside the method?
- A. null
- B. An empty array of length 0
- C. An array containing one 0
- D. The call does not compile
Show answer
B. With no arguments the compiler passes an empty array, so nums.length is 0 and no null check is needed for this call.
Interview questions
How do varargs interact with method overloading?
Show answer
A varargs method is considered last. The compiler first tries to match without boxing or varargs, then with boxing, and only then with varargs. So if both print(int a, int b) and print(int... values) exist, the call print(1, 2) uses the two-parameter method.
What is the difference between declaring a parameter as int... and as int[]?
Show answer
Inside the method they are the same: both are an int[]. The difference is at the call site. With int[] the caller must create and pass an array. With int... the caller can pass an array, or list the values separated by commas, or pass nothing, and the compiler builds the array.
Key Points to Remember
- Varargs syntax is Type... name, usable as an array inside the method body.
- A method may have only one varargs parameter, and it must be the last parameter.
- Callers can pass individual values, an array, or nothing at all.
- Java prefers a matching fixed-arity overload over a varargs overload when both are applicable.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Runs plain Java 17. Frameworks such as Spring, and databases, are not available here. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering