Skip to content

Varargs (variable‑length arguments)

Varargs allow a method to accept zero or more arguments of the same type. The syntax uses an ellipsis (...) after the type.

Syntax:

returnType methodName(Type... parameterName) { ... }

Internally, the varargs parameter is treated as an array.

Example:

int sum(int... numbers) {
int total = 0;
for (int n : numbers) {
total += n;
}
return total;
}
System.out.println(sum(1, 2, 3)); // 6
System.out.println(sum()); // 0
System.out.println(sum(10)); // 10

Rules:

  • Only one varargs parameter per method.
  • It must be the last parameter.
void example(String prefix, int... values) { } // correct
// void example(int... values, String prefix) { } // error

Overloading with varargs – can be ambiguous; use with care.

void print(int... a) { }
void print(int a, int... b) { }
print(1); // ambiguous: matches both? Actually second is more specific? Java rules.

Use cases: Logging, formatting, mathematical operations with variable inputs.