Variable Arguments (Varargs) in Java
Complete guide to varargs usage, rules, pitfalls, and best practices.
Table of Contents
- Example: Varargs in Action
- 1. What Are Varargs?
- 2. Why Varargs Are Useful
- 3. How Varargs Work Internally
- 4. Calling Vararg Methods
- 5. Rules of Varargs (Must Remember)
- 6. Varargs with Other Parameters
- 7. Varargs and Enhanced For Loop
- 8. Varargs vs Method Overloading
- 9. Common Pitfalls
- 10. When NOT to Use Varargs
- 11. One-Line Mental Model
- 12. Interview Carry-Forward Points
Example: Varargs in Action
public class Calculation {
public int sum(int... numbers) {
int total = 0;
for (int number : numbers) {
total += number;
}
return total;
}
// ❌ Only one vararg parameter is allowed
// public int sum2(int... numbers, int... numbers2) { }
// ❌ Vararg must be the last parameter
// public int sum3(int... numbers, int x) { }
public int squareSum(
int x,
boolean flag,
int... numbers
) {
int total = 0;
for (int number : numbers) {
total += (int) Math.pow(number, x);
}
return total;
}
}
public class VarArgsDemo {
public static void main(String[] args) {
Calculation calcObj = new Calculation();
int total = calcObj.sum(1, 3);
System.out.println(total);
total = calcObj.sum(1, 3, 5, 9, 10, 4, 4, 4, 4);
System.out.println(total);
total = calcObj.squareSum(2, true, 3, 4, 1, 2, 3);
System.out.println(total);
}
}
1. What Are Varargs?
Varargs allow a method to accept zero or more arguments of the same type.
public int sum(int... numbers) { }
📌 Meaning: The method can be called with any number of int values.
2. Why Varargs Are Useful
Without varargs:
- Multiple overloaded methods are needed
- Code becomes repetitive
With varargs:
- Single method handles all cases
- API becomes clean and flexible
3. How Varargs Work Internally
sum(1, 2, 3);
Internally treated as:
sum(new int[]{1, 2, 3});
📌 Key Insight: Varargs are syntactic sugar over arrays.
4. Calling Vararg Methods
sum(); // allowed (empty array)
sum(1); // one argument
sum(1, 2, 3, 4); // multiple arguments
All calls are valid.
5. Rules of Varargs (Must Remember)
Rule 1: Only One Vararg Parameter
// ❌ Not allowed
void method(int... a, int... b);
Rule 2: Vararg Must Be Last
// ❌ Not allowed
void method(int... a, int x);
// ✅ Allowed
void method(int x, int... a);
6. Varargs with Other Parameters
public int squareSum(int x, boolean flag, int... numbers) { }
📌 Rule: Varargs must be the last parameter, but other parameters are allowed before it.
7. Varargs and Enhanced For Loop
int total = 0;
for (int n : numbers) {
total += n;
}
- Varargs integrate naturally with loops
- Improves readability
8. Varargs vs Method Overloading
| Feature | Varargs | Overloading |
|---|---|---|
| Number of methods | One | Many |
| Flexibility | High | Limited |
| Readability | Better | Can get cluttered |
📌 Best practice: Prefer varargs when the number of parameters is variable.
📎 See also: Java Methods – Method Overloading
9. Common Pitfalls
- Passing
nullcan causeNullPointerException - Ambiguous calls with overloading
- Performance overhead (array creation)
10. When NOT to Use Varargs
- When exact number of parameters is fixed
- Performance-critical loops
- When argument meaning depends on position
11. One-Line Mental Model
Varargs = flexible method parameters backed by arrays
12. Interview Carry-Forward Points
- Varargs allow variable number of arguments
- Implemented using arrays internally
- Only one vararg per method
- Must be the last parameter
- Improves API usability
Next natural topics: method overloading with varargs, ambiguity between overloaded methods, performance implications, varargs with generics.
Tell me what you want to learn next 👍