Variable length arguments (Varargs) in Java

Java allows a method to take variable number of arguments as input. Such a method is called a vararg (short for variable argument) method. It can take 0 or more arguments as input.


1. Syntax

A vararg method differs from any normal method in arguments only. In the argument list data-type is followed by three dots(also called ellipses), then variable name appears.


2. A Vararg Method stores the arguments in an Array

Internally, the variable that accepts the variable number of arguments is implemented as an array:

  • In the argument list data-type is followed by ellipses, then variable name appears which stores the arguments in an array.
  • Inside the method, this variable can be accessed like any other array.

Output :

 


3. Vararg vs Array as an argument

Both are equally powerful.

  • For a method that takes an array as the argument. you have to pack all the arguments in an array and pass this array to the method.
  • For a vararg method, you can directly pass the arguments to the method, you don’t have to pack them in an array. Java handles the packaging for you.

Let’s look at the last example using an array:

Output :

Note that there is virtually nothing that can be done by Varargs which cannot be done by a method that takes an array. It all depends on an individual’s preference. Infact, before varargs were introduced in Java 5, arrays were used to handle variable number of arguments.


4. A Vararg Method can take other Arguments

A vararg method can take ordinary arguments along with vararg arguments. In such cases, the vararg argument must be the rightmost argument.

Output :

  • “first 7 integers” is passed to String str.
  • Rest integers are passed to vararg v as an array.

4.1 One more example

If a method takes ordinary arguments, then the way java compiler works is:

  • It starts from left most argument and matches the arguments one by one to the method parameter. The rest of arguments are passed to the vararg paramter.

Output :


5. Overloading Vararg Method

Like any other method in Java, a vararg method can be overloaded as well. A vararg method can be overloaded with :

  • Ordinary methods
  • Other vararg methods

5.1 Overloading vararg with ordinary methods

The way compiler resolves which method to call by first finding an exact match wherever it can find. If the exact match is not found then vararg kicks in.

Output :

There is an exact match for meth(5). So that definition of method meth is called. For meth() and meth(5, 7) vararg method is called.


5.2 Overloading two vararg methods

Output :


6. Overloading and Ambiguity

If possible, avoid overloading a vararg method, it often leads to ambiguous method calls.

  • Ambiguity often arises when you overload a vararg method with another vararg method. Take following code for example:


  • If you overload a vararg method with an ordinary method, compiler resloves the call by preferring the ordinary method. So it is not ambiguous. Following code is not ambiguous.


6.1 Example

Let’s look at a concrete example to understand the ambiguity.

Output :


7. Precautions

  • There can be only one vararg in a parameter-list of a method.


  • Only last argument of a method can be vararg.

6. Miscellaneous

  • Vararg helps you avoid overloading to some extent. For example you do not need to write two separate methods to find the sum of 2 and 3 numbers respectively. Instead you can write a single vararg method like:

    Output :

     
  • Before varargs were introduced, people wrote code using
    • Overloaded methods or
    • Arrays