Java Varargs — Free Java Tutorial
Learn Java Varargs in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
TL;DR: Learn Java Varargs in Java with a free, beginner-friendly tutorial, examples and practice for Indian students on Syllab.in.
Written & reviewed by the Syllab.in Academic Team (CBSE/NCERT subject experts) · Updated
Java Varargs in Java
Varargs (variable arguments) allow a method to accept any number of arguments of a specified type. Declared with three dots: type... paramName.
Inside the method, varargs are treated as an array. You can iterate over them, check length, and use array methods.
A method can only have one varargs parameter, and it must be the last parameter.
You can call a varargs method with zero arguments, individual arguments, or pass an array directly.
Java Varargs — Syntax
// Varargs declaration
void printAll(String... items) {
for (String item : items) System.out.println(item);
}
// Calling with varargs
printAll(); // 0 args
printAll("Hello"); // 1 arg
printAll("a", "b", "c"); // 3 args
printAll(new String[]{"x","y
Learn Java Varargs step by step with Syllab's free interactive Java tutorial — runnable code examples, practice exercises and instant AI feedback, all free with no signup. Explore the full Java course →