Java Varargs

Java varargs let a method accept zero or more arguments of the same type. The word varargs means variable-length arguments.

Use varargs when a method should work with any number of values without forcing the caller to create an array manually.

Varargs Syntax

A varargs parameter uses three dots after the type:

public class Main {
    static void printNames(String... names) {
        for (String name : names) {
            System.out.println(name);
        }
    }

    public static void main(String[] args) {
        printNames("Mia", "Noah", "Ava");
    }
}

Output:

Mia
Noah
Ava

The parameter String... names means the method can receive any number of String arguments. Inside the method, Java treats names like a String[] array.

Calling A Varargs Method

You can call a varargs method with several arguments, one argument, no arguments, or an existing array of the same type.

public class Main {
    static int sum(int... numbers) {
        int total = 0;

        for (int number : numbers) {
            total += number;
        }

        return total;
    }

    public static void main(String[] args) {
        System.out.println(sum(4, 6, 10));
        System.out.println(sum(5));
        System.out.println(sum());

        int[] scores = {7, 8, 9};
        System.out.println(sum(scores));
    }
}

Output:

20
5
0
24

The call sum() is allowed because varargs can receive zero values. In that case, numbers is an empty array, so the loop runs zero times and the total stays 0.

Varargs Are Arrays Inside The Method

Because a varargs parameter behaves like an array inside the method, you can use length, indexes, and enhanced for loops with it.

public class Main {
    static void showItems(String label, String... items) {
        System.out.println(label + ": " + items.length);

        if (items.length > 0) {
            System.out.println("First: " + items[0]);
        }
    }

    public static void main(String[] args) {
        showItems("Tools", "hammer", "saw");
        showItems("Empty list");
    }
}

Output:

Tools: 2
First: hammer
Empty list: 0

Before reading an item by index, check items.length. If a caller passes no varargs values, there is no first element.

Varargs Must Come Last

A method can have regular parameters before a varargs parameter. However, the varargs parameter must be the last parameter in the list.

For example, static void showItems(String label, String... items) is valid because items comes last. A method cannot put another parameter after String... items, because Java would not know where the variable-length argument list ends.

When To Use Varargs

Varargs are useful for small helper methods such as printing a group of values, adding numbers, building messages, or checking several options. They keep calls readable when the number of arguments naturally changes.

If the values already belong together as one collection, an array or another collection type may still be clearer. Varargs are best when the caller should be able to pass values directly.

Common Mistakes

  • Putting a varargs parameter before another parameter.
  • Forgetting that the varargs parameter may be empty.
  • Trying to use more than one varargs parameter in the same method.
  • Using varargs when a fixed number of parameters would be simpler and clearer.

Takeaway: Java varargs use ... to let one method accept any number of same-type arguments, and inside the method those arguments are handled like an array.