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.

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.

✓ 100% Free ✓ No Login Needed ✓ NCERT / CBSE Aligned ✓ Download as PDF

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

🤖 Stuck on any question? Ask Syllab's free AI Tutor for a step-by-step explanation — instant, unlimited, no login.

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"});   // array

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 →