Topic 110 of 119 14. Additional Java Topics Interactive Tutorial

Java Varargs: Variable-Length Argument Lists in Method Declarations

10 mins read Last updated: 22 Aug 2026 16,030 views

Direct Answer & Core Concept

Java Varargs, short for variable-length arguments, allow a method to accept zero, one, or many arguments of the same type without requiring the caller to create an array manually. Varargs are useful when you do not know in advance how many values a method will receive.

The feature uses the ... syntax in a method parameter declaration. Inside the method, the varargs parameter behaves like an array, which makes it easy to loop through and process the supplied values.

For example, instead of creating separate methods for one number, two numbers, or five numbers, you can create one method that accepts any number of integers.

Main.java
Run Online
public static int add(int... numbers) {
    int total = 0;
    for (int number : numbers) {
        total += number;
    } return total;
}

The same method can now be called with different numbers of arguments:

Main.java
Run Online
System.out.println(add());
System.out.println(add(10));
System.out.println(add(10, 20));
System.out.println(add(10, 20, 30, 40));

Detailed Explanation & Mechanics

What Is Varargs in Java?

A varargs parameter allows a method to receive a variable number of arguments. It is declared by placing three dots (...) after the parameter type.

Main.java
Run Online
returnType methodName(dataType... parameterName) {
    // method body
}

For example:

Main.java
Run Online
public static void printNumbers(int... numbers) {
    for (int number : numbers) {
        System.out.println(number);
    }
}

The method can accept any number of int values, including no values.

Main.java
Run Online
printNumbers();
printNumbers(10);
printNumbers(10, 20, 30);

Java internally treats the varargs parameter as an array.


Why Use Varargs?

Without varargs, you may need to create multiple overloaded methods when the number of arguments can change.

For example:

Main.java
Run Online
public static int add(int a, int b) {
    return a + b;
}public static int add(int a, int b, int c) {
    return a + b + c;
}public static int add(int a, int b, int c, int d) {
    return a + b + c + d;
}

This becomes inconvenient if callers may provide many values.

With varargs, one method can handle all these cases:

Main.java
Run Online
public static int add(int... numbers) {
    int total = 0;
    for (int number : numbers) {
        total += number;
    } return total;
}

Now the same method works with different argument counts.


Varargs Syntax in Java

The basic syntax is:

Main.java
Run Online
dataType... parameterName

For example:

Main.java
Run Online
public static void display(String... names) {
    for (String name : names) {
        System.out.println(name);
    }
}

The ... is called the varargs notation.

A method can have only one variable-arity parameter, and that parameter must be the last parameter in the method declaration.


Simple Java Varargs Example

Here is a basic example that calculates the sum of numbers.

Main.java
Run Online
public class VarargsExample {
    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(10, 20));
        System.out.println(sum(10, 20, 30));
        System.out.println(sum(5, 10, 15, 20, 25));
    }
}

Output:

The caller does not need to create an array explicitly.


Varargs Can Accept Zero Arguments

One useful property of varargs is that the caller can provide no arguments.

Main.java
Run Online
public static void show(int... numbers) {
    System.out.println("Number of arguments: " + numbers.length);
}

Calling:

produces:

In this case, Java supplies an empty array to the method.

This makes varargs useful for methods where providing values is optional.


Varargs Can Accept One Argument

A varargs method can also receive exactly one value.

Main.java
Run Online
public static void show(int... numbers) {
    System.out.println(numbers[0]);
}

Calling:

prints:

The method receives the value through an array containing one element.


Varargs Can Accept Multiple Arguments

You can provide as many compatible arguments as needed.

Main.java
Run Online
public static void show(int... numbers) {
    for (int number : numbers) {
        System.out.println(number);
    }
}

Example:

Main.java
Run Online
show(10, 20, 30, 40, 50);

The values are available inside the method as an array.

Conceptually, the call behaves similarly to passing an array:

Main.java
Run Online
show(new int[] {
    10, 20, 30, 40, 50
});

Varargs Are Internally Arrays

A varargs parameter is represented as an array inside the method.

Consider:

Main.java
Run Online
public static void display(String... values) {
    System.out.println(values.getClass());
}

The parameter values behaves like a String[].

You can use:

and array indexing:

You can also use enhanced for loops:

Main.java
Run Online
for (String value : values) {
    System.out.println(value);
}

Therefore, learning arrays makes varargs easy to understand.


Passing an Array to a Varargs Method

You can pass an existing array directly to a compatible varargs parameter.

Main.java
Run Online
public static void printNumbers(int... numbers) {
    for (int number : numbers) {
        System.out.println(number);
    }
}

Now create an array:

Main.java
Run Online
int[] values = {
    10, 20, 30, 40
};
printNumbers(values);

This works because int... is handled as int[] inside the method.

You can therefore call the same method using individual arguments or an array.

Main.java
Run Online
printNumbers(10, 20, 30);
int[] values = {
    40, 50, 60
};
printNumbers(values);

Varargs With Other Parameters

A method can have normal parameters before the varargs parameter.

For example:

Main.java
Run Online
public static void printMessage(String message, int... numbers) {
    System.out.println(message);
    for (int number : numbers) {
        System.out.println(number);
    }
}

Calling:

Main.java
Run Online
printMessage("Numbers:", 10, 20, 30);

produces:

The fixed parameter comes first, followed by the variable-length parameter.


Varargs Must Be the Last Parameter

The varargs parameter must appear at the end of the parameter list.

Correct:

Main.java
Run Online
public static void test(String name, int... numbers) {
}

Incorrect:

Main.java
Run Online
public static void test(int... numbers, String name) {
}

The second declaration is invalid because Java needs to know where the variable-length arguments end.

For example, this would be ambiguous:

Main.java
Run Online
test(10, 20, 30, "John");

Java would not have a clear way to determine which values belong to the varargs parameter.


Only One Varargs Parameter Is Allowed

A method cannot have two variable-length parameters.

This is invalid:

Main.java
Run Online
public static void test(int... numbers, String... names) {
}

Java allows only one varargs parameter in a method declaration.

A typical valid declaration is:

Main.java
Run Online
public static void test(String name, int... numbers) {
}

Here, name is fixed and numbers is variable-length.


Varargs With Different Data Types

Varargs work with reference types as well as primitive types.

Integer Varargs

Main.java
Run Online
public static void numbers(int... values) {
    for (int value : values) {
        System.out.println(value);
    }
}

String Varargs

Main.java
Run Online
public static void names(String... names) {
    for (String name : names) {
        System.out.println(name);
    }
}

Call:

Main.java
Run Online
names("Alice", "Bob", "Charlie");

Double Varargs

Main.java
Run Online
public static void prices(double... prices) {
    for (double price : prices) {
        System.out.println(price);
    }
}

The component type of the varargs determines what values can be supplied.


Varargs and Primitive Types

Varargs can directly use primitive types.

For example:

Main.java
Run Online
public static int calculate(int... numbers) {
    int total = 0;
    for (int number : numbers) {
        total += number;
    } return total;
}

This method receives an int[] internally.

Other primitive types can also be used:

Main.java
Run Online
double...long...float...boolean...char...byte...short...

For example:

Main.java
Run Online
public static void display(double... values) {
    for (double value : values) {
        System.out.println(value);
    }
}

Varargs and Object Types

You can also use a reference type as the varargs component type.

Main.java
Run Online
public static void printObjects(Object... objects) {
    for (Object object : objects) {
        System.out.println(object);
    }
}

Now different reference and boxed values can be supplied:

Main.java
Run Online
printObjects("Java", 100, 25.5, true);

Autoboxing allows primitive values such as 100 and 25.5 to be represented as wrapper objects when passed to an Object... parameter.


Varargs and Method Overloading

Varargs participate in Java's method-overloading rules.

Consider:

Main.java
Run Online
public static void show(int value) {
    System.out.println("Single int");
}public static void show(int... values) {
    System.out.println("Varargs");
}

Now:

The fixed-arity method is selected because it is a more specific direct match than the variable-arity alternative.

But:

can use the varargs method.

When overloading methods with varargs, be careful because combinations of overloaded methods can create ambiguity.


Varargs and Ambiguous Method Calls

Poorly designed overloads can produce ambiguous calls.

For example:

Main.java
Run Online
public static void test(int... values) {
}public static void test(boolean... values) {
}

Now this call:

does not provide enough information for Java to determine which method should be selected.

Both methods can accept zero arguments.

The compiler therefore reports an ambiguity error.

This is one reason to avoid unnecessary varargs overloads with unrelated parameter types.


Varargs With Null

Because a varargs parameter is an array internally, null can be relevant.

For example:

Main.java
Run Online
public static void print(String... values) {
    System.out.println(values == null);
}

This is valid:

The method receives a null array rather than an empty array.

That is different from:

where the method receives an empty array.

Therefore, code should not automatically assume a varargs array is non-null if callers can explicitly pass a null array.


Calling a Varargs Method With No Values vs Null

These two calls are conceptually different:

and:

The first represents an empty argument list.

The second explicitly passes a null array.

For example:

Main.java
Run Online
public static void print(String... values) {
    if (values == null) {
        System.out.println("Null array");
        return;
    } System.out.println("Arguments: " + values.length);
}

Output for:

is:

While:

prints:


Varargs and Generic Types

Varargs can be used with generic reference types.

For example:

Main.java
Run Online
public static <T> void print(T... values) {
    for (T value : values) {
        System.out.println(value);
    }
}

You can call:

Main.java
Run Online
print("Java", "Python", "Go");

or:

However, generic varargs can sometimes produce compiler warnings because of the interaction between arrays, generics, and type erasure.


Heap Pollution and Generic Varargs

Generic varargs require additional care because Java arrays retain runtime component-type information while generic type arguments are erased.

For example:

Main.java
Run Online
static <T> void display(T... values) {
    for (T value : values) {
        System.out.println(value);
    }
}

Depending on how such a method is implemented or called, the compiler may issue an unchecked or unsafe operation warning.

The issue is related to heap pollution, where a variable of a parameterized type may refer to an object that is not actually of the expected parameterized type.

Do not suppress such warnings blindly. First verify that the method is type-safe.


@SafeVarargs

Java provides the @SafeVarargs annotation for certain methods whose implementation is known to be safe despite using generic varargs.

For example:

Main.java
Run Online
@SafeVarargspublic static <T> void print(T... values) {
    for (T value : values) {
        System.out.println(value);
    }
}

@SafeVarargs tells the compiler and readers that the method has been reviewed for unsafe generic-varargs operations.

It should not be used simply to hide warnings. The method should actually be safe.

The annotation is applicable to appropriate static, final, and private instance methods, where overriding cannot introduce an unsafe implementation.


Varargs vs Array Parameter

These declarations look similar but provide different calling syntax.

Array Parameter

Main.java
Run Online
public static void print(int[] values) {
}

Call:

Main.java
Run Online
int[] values = {
    10, 20, 30
};
print(values);

You cannot directly call:

Varargs Parameter

Main.java
Run Online
public static void print(int... values) {
}

Now both forms are possible:

and:

Main.java
Run Online
int[] values = {
    10, 20, 30
};
print(values);

So varargs provide more convenient calling syntax while the method still receives an array.


Varargs vs Method Overloading

Suppose you want to support different numbers of values.

Without varargs:

Main.java
Run Online
sum(int a)sum(int a, int b)sum(int a, int b, int c)sum(int a, int b, int c, int d)

With varargs:

The second approach is usually much easier to maintain when the operation naturally accepts an arbitrary number of values.

However, varargs should not replace meaningful overloads when different argument counts have different semantics.


Example: Finding the Maximum Number

Varargs are useful for utility methods such as finding the largest number.

Main.java
Run Online
public class MaximumExample {
    static int maximum(int... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException("At least one number is required");
        } int max = numbers[0];
        for (int number : numbers) {
            if (number > max) {
                max = number;
            }
        } return max;
    } public static void main(String[] args) {
        System.out.println(maximum(10, 50, 20));
        System.out.println(maximum(5, 100, 25, 75));
    }
}

Output:

Notice that the method explicitly checks for an empty argument list because the algorithm requires at least one value.


Example: Calculating an Average

Here is another practical example:

Main.java
Run Online
public static double average(double... values) {
    if (values.length == 0) {
        throw new IllegalArgumentException("At least one value is required");
    } double total = 0;
    for (double value : values) {
        total += value;
    } return total / values.length;
}

Call:

Main.java
Run Online
System.out.println(average(10, 20, 30));

Output:

Varargs make this method convenient because the caller does not need to manually construct a double[].


Example: Building a Message

Varargs are not limited to mathematical operations.

Main.java
Run Online
public static String joinWords(String... words) {
    return String.join(" ", words);
}

Call:

Main.java
Run Online
String message = joinWords( "Java", "is", "easy", "to", "learn");
System.out.println(message);

Output:

This demonstrates how varargs can simplify utility methods that accept a flexible number of values.


Performance Considerations

Varargs are convenient, but they are not completely free.

When individual arguments are supplied, Java needs to package them into an array for the method invocation.

For example:

conceptually involves an array containing those values.

For normal application code, this overhead is usually insignificant. However, in extremely performance-sensitive code that executes millions of times, unnecessary temporary arrays and boxing can matter.

In such cases, measure the actual workload before changing a clean varargs API.


Varargs With Boxing

Primitive varargs and reference varargs have different performance characteristics.

For example:

Main.java
Run Online
static void process(int... values) {
}

uses an int[].

But:

Main.java
Run Online
static void process(Integer... values) {
}

uses an Integer[].

Passing primitive integers to the second method can involve boxing:

The values may need to become Integer objects.

Therefore, use primitive varargs when you specifically need primitive values and want to avoid unnecessary boxing.


Common Mistakes With Java Varargs

Mistake 1: Putting Varargs Before Another Parameter

Incorrect:

Main.java
Run Online
void test(int... numbers, String name) {
}

The varargs parameter must be last.


Mistake 2: Declaring Multiple Varargs Parameters

Incorrect:

Main.java
Run Online
void test(int... numbers, String... names) {
}

A method can have only one variable-arity parameter.


Mistake 3: Assuming Varargs Are Not Arrays

Inside the method:

Main.java
Run Online
void test(int... values) {
}

values behaves as an int[].

You can use:

and enhanced for loops.


Mistake 4: Forgetting the Empty Case

A varargs method can receive zero arguments:

If the algorithm requires at least one value, validate the length:

Main.java
Run Online
if (values.length == 0) {
    throw new IllegalArgumentException("At least one value is required");
}

Mistake 5: Using Generic Varargs Without Understanding the Warning

Generic varargs can produce unchecked warnings because of type erasure and arrays.

Do not automatically add:

Main.java
Run Online
@SuppressWarnings("unchecked")

or:

without understanding why the warning occurs and whether the implementation is actually safe.


Java Varargs Best Practices

Use varargs when the method naturally accepts a variable number of arguments and all arguments have the same logical type.

Keep the varargs parameter at the end and avoid creating confusing overload combinations that can make calls ambiguous.

Validate the empty case when your algorithm requires at least one value, and be careful with null arrays and generic varargs.

For performance-sensitive code, remember that varargs can involve array creation and that wrapper-type varargs can involve boxing.


Varargs Example for a Utility Class

Here is a complete example combining several concepts:

Main.java
Run Online
public class MathUtils {
    public static int sum(int... numbers) {
        int total = 0;
        for (int number : numbers) {
            total += number;
        } return total;
    } public static int maximum(int... numbers) {
        if (numbers.length == 0) {
            throw new IllegalArgumentException( "At least one number is required");
        } int max = numbers[0];
        for (int number : numbers) {
            if (number > max) {
                max = number;
            }
        } return max;
    } public static void main(String[] args) {
        System.out.println("Sum: " + sum(10, 20, 30));
        System.out.println("Maximum: " + maximum(15, 70, 25, 40));
    }
}

Output:

This is a typical use case for varargs in reusable utility methods.


Key Rules of Java Varargs

RuleExplanation
Syntaxtype... name
Internal representationArray
Number of argumentsZero or more
PositionMust be the last parameter
Number of varargs parametersOnly one
Arrays acceptedYes, compatible array can be passed
Primitive typesSupported
Reference typesSupported
Generic typesSupported with additional safety considerations
Empty invocationAllowed
Main benefitConvenient variable-length method calls

Frequently Asked Questions

What is varargs in Java?

Varargs allow a Java method to accept a variable number of arguments. They are declared using three dots, such as int... numbers.

What does ... mean in Java?

In a method parameter declaration, ... indicates a variable-arity parameter, also called varargs.

Is a varargs parameter an array?

Yes. Inside the method, a varargs parameter is represented and accessed as an array.

Can a varargs method accept zero arguments?

Yes.

Main.java
Run Online
void test(int... values) {
}

The method can be called as:

Can I pass an array to a varargs method?

Yes.

Main.java
Run Online
int[] values = {
    10, 20, 30
};
test(values);

Can a method have two varargs parameters?

No. Java allows only one variable-arity parameter, and it must be the final parameter.

Can varargs be followed by another parameter?

No.

Correct:

Main.java
Run Online
void test(String name, int... numbers)

Incorrect:

Main.java
Run Online
void test(int... numbers, String name)

Are varargs faster than arrays?

Varargs are primarily a convenience feature. When individual arguments are supplied, Java packages them into an array, so there can be allocation overhead. For most normal code, this is not a concern.

What is the difference between int... and int[]?

Inside the method, both represent an int array. The important difference is calling syntax: int... allows individual arguments such as method(1, 2, 3) as well as an int[].

What is @SafeVarargs?

@SafeVarargs can be used on eligible methods to indicate that their generic varargs implementation has been checked and does not perform unsafe operations with the varargs array.

Suggested Graphics and Page Design

Graphic 1 — Varargs Concept

Create a simple diagram:

Architecture Diagram
                 Java Varargs
                      |
                 int... numbers
                      |
        ┌─────────────┼─────────────┐
↓

↓

↓
10, 20      10, 20, 30    No values
        |             |             |
        └─────────────┼─────────────┘
↓
int[]

Caption:

Java varargs allow a method to receive zero or more arguments as an array.

Graphic 2 — Parameter Position

Show:

Main.java
Run Online
Correct:method(String name, int... numbers) ↑ LASTIncorrect:method(int... numbers, String name) ↑ ↑ VARARGS AFTER VARARGS

Graphic 3 — Varargs vs Array

Use a side-by-side comparison:

Main.java
Run Online
Arraymethod(int[] values)↓method(new int[] {
    10, 20, 30
})Varargsmethod(int... values)↓method(10, 20, 30)

Runnable Code Example: Java Varargs

Java 17+
Main.java
Run Online
public class VarargsDemo {
    public static int sumNumbers(String label, int... values) {
        int total = 0;
        for (int v : values) {
            total += v;
        } System.out.println(label + ": " + total);
        return total;
    } public static void main(String[] args) {
        sumNumbers("Zero items");
        sumNumbers("Two items", 10, 20);
        sumNumbers("Four items", 1, 2, 3, 4);
    }
}

Common Java Technical Interview Questions & Answers

Interview Prep
Q1

Can a method have two varargs parameters in Java?

No, a method can have at most one varargs parameter, and it must be the last parameter in the signature.
Q2

What is heap pollution in generic varargs?

When a generic array parameter references an object that is not of that type, potentially causing a ClassCastException at runtime.

Key Takeaways, Best Practices & Clean Code Rules

Java Best Practice & Production Rules

Use varargs for logging utilities, string formatting, and mathematical operations that accept variable operand counts.