Category: java
Vararg method
Published on 22 Feb 2026
Explanation
Varargs allow a method to accept a
variable number of arguments.
Code:
void display(int... numbers) {
for(int n : numbers) {
System.out.println(n);
}
}
Explanation
Varargs are treated as arrays inside the method.
Code:
void sum(int... values) {
System.out.println(values.length);
}
Explanation
Varargs parameter must be the last
parameter in the method.
Code:
void example(String name, int... marks) { }
Explanation
You can call varargs method with zero or
more arguments.
Code:
display(); display(1, 2, 3);
Explanation
Only one varargs parameter is allowed in a
method.
Code:
// void test(int... a, int... b) {} // Not allowed