Last Minute Java VarArgs or Variable Arguments Explained with Examples Tutorial

Java Varargs or Variable Arguments Infographic

Java Varargs refer to Variable Arguments. Java Varargs are applicable only for Constructors and Methods. Let us know more in this Last Minute Java Tutorial.

Java Varargs or Variable Arguments Explained

Java Varargs allow writing constructors or methods that accept any unknown (arbitrary) number of arguments. The number of arguments may be zero also. These Java Variable Arguments successfully implement Method Overloading or Constructor Overloading by maintaining unique method signatures.

You can make any Variable of Primitive or Object type, a Variable Arguments (Varargs) type. To do this, you need to add Three Dots (...) next to the Data-Type within the Parameters List of a method or constructor.

A Java Vararg variable is treated as an Array. A Vararg variable can come only in the last position in a parameter list. In a single Method or Constructor, only one Varargs type variable can be defined.

Java Varargs in a Constructor

The syntax is as follows.

CONSTRUCTOR_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Constructor's Parameter List.

public class VarargsExample
{
  VarargsExample(int... a)
  {
    if(a.length == 0)
      System.out.println("Zero Arguments");
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
    System.out.println();
  }

  VarargsExample(String name, int...a)
  {
    System.out.println("Name= " + name);
    for(int i=0; i<a.length; i++)
      System.out.print(a[i] + ",");
  }

  public static void main(String[] args)
  {
    VarargsExample obj = new VarargsExample();
    VarargsExample ve = new VarargsExample(5,6,7);
    VarargsExample ve2 = new VarargsExample("CANADA", 1,2);
  }
}
//OUTPUT
Zero Arguments

5,6,7,
Name= CANADA
1,2,

Notice that we have not maintained a Zero-Argument constructor in the above example. Even then, the constructor with int...a arguments is invoked for new VarargsExample() call. If you put a constructor with zero arguments, it will be invoked instead of Variable Arguments type constructor if it is the first constructor matched for zero arguments.

Let us check another example that shows incompatible constructor overloading using Varargs.


public class VarargsExample
{
  VarargsExample(boolean...a)
  {

  }
  VarargsExample(int... a)
  {

  }
  public static void main(String[] args)
  {
    VarargsExample ve = new VarargsExample(); //Error
  }

}
//OUTPUT
Compiler error

The above example produces an error "The Constructor is Ambiguous". Because int and boolean are incompatible or inconvertible data types. If we replace boolean with a number data type like float, double, long or byte, the program compiles and gives the output.

Note: While choosing a Vararg constructor, the compiler selects the one with a small-sized data type first. So CONSTRUCTOR(byte...a) is selected before CONSTRUCTOR(int...a).

Java Varargs in a Method

Just like in a Constructor with Varargs, Java methods too can have Varargs. Only one Vararg type variable is allowed in a method that too at the end of the parameters list.

The syntax is as follows.

METHOD_NAME(TYPE... VARIABLE_NAME)
{
  //CODE for initialization
}

Check the below example that implements Varargs inside a Method's Parameter List.


public class VarargsMethodExample
{
  void show(String...names)
  {

  }
  void show(int...codes)
  {

  }
  void modify(String operator, long...transmission)
  {

  }
  String report(float var, int...codes)
  {
    String a = "";
    for(int i=0; i<codes.length; i++)
    {
      a = a + codes[i] + "," ;
    }
    return a;
  }

  public static void main(String[] args)
  {
    VarargsMethodExample vme = new VarargsMethodExample();
    String rep = vme.report(0.0f, 2,3,4);
    System.out.println("Report= " + rep);
  }
}
//OUTPUT
Report= 2,3,4,

In the above example, we have used Java Varargs in the methods show(), modify() and report(). Method "show" is overloaded. Method "report" even returns a value.

Share this Last Minute Java Varargs (Variable Arguments) Tutorial with your friends and colleagues to encourage authors.