Varargs (Variable length arguments) in java -


Variable length arguments allows us to define methods that can have variable number of arguments and such methods are called varargs methods. This feature has been added in java in jdk1.5.

Syntex -  data_type... variable_name
Here data_type can be any valid java data type and it is followed by 3 periods (.) and after that there will be name for varargs. For example -
public void demo(int... x)
{
  //method body
}
The argument passed in the method is variable length argument and it tells the compiler that this method can be called by using 0 or more arguments of integer type.
Here x is implicitly declared as an array of int data type. So we can iterate it just like an array and can also use arrays length property. But it is more flexible then array as it can take any number of arguments, we can even pass zero number of arguments.

Example -

class Test
{
    int s = 0;
    public void demo(int... x)
    {
        System.out.println("Number of arguments - "+x.length);
        System.out.print("Arguments are - ");
        for(int t:x)
        {
            System.out.print(t+"  ");
        }
        System.out.println();
    }
}
public class Call {

    public static void main(String[] args) {

        Test t1  = new Test();
        t1.demo(1,2,3,4,5);
        t1.demo(1,2);
        t1.demo();    
    }
       
} 

        

Output -

Number of arguments - 5
Arguments are - 1  2  3  4  5  
Number of arguments - 2
Arguments are - 1  2  
Number of arguments - 0
Arguments are ?

        
If a method has more than one argument then variable length argument should be at the end of right hand side of all the arguments declared (i.e. it should be last argument in method).
Also remember that we cannot have more than one varargs in any method declaration. Below examples shows the use of varargs with multiple arguments -
public void demo(int a, int b, int... c)
{
 // method body
}

     
In this declaration, the first two arguments used to call the methods will belong to first two parameters and remaining arguments will belongs to varargs c.
public void demo(int... a, int b, int c)
{
  // method body
}

 
The above syntax is incorrect, because varargs should be the last parameter.
public void demo(int a, int b, int... c, float... d)
{
  // method body
}

The above syntax is also incorrect because we cannot have more than one varargs in method declaration.

Example -

class Test
{
    int s = 0;
    public void demo(String s, int... x) // varargs should be at the end
    {
        
        System.out.println("String - "+s);
        System.out.println("Number of arguments - "+x.length);
        System.out.print("Arguments are - ");
        for(int t:x)
        {
            System.out.print(t+"  ");
        }
        System.out.println();
    }
}
public class Call {

    public static void main(String[] args) {

        Test t1  = new Test();
        t1.demo("Hello", 1,2,3,4,5);
        t1.demo("Hi",1,2);
        t1.demo("Bye");    
    }
       
}

Output -

String - Hello
Number of arguments - 5
Arguments are - 1  2  3  4  5  
String - Hi
Number of arguments - 2
Arguments are - 1  2  
String - Bye
Number of arguments - 0
Arguments are ?

Overloading Varargs Methods -

You can overload a method that take varargs as follows -
class Test
{
 public int sum(int... a)
 {
     int s =0;
     for(int val:a)
     {
         s=s+val;
     }
     return s;
 }
 public double sum(double... a)
 {
     double s =0;
     for(double val:a)
     {
         s=s+val;
     }
     return s;
 }
 
}
public class Call {

    public static void main(String[] args) {

        Test t1  = new Test();
        System.out.println("Sum of Integers :" + t1.sum(10,20,30,40));
        System.out.println("Sum of Doubles :" + t1.sum(10.5,20.23,30.45,40.67));
             
    }
       
}

Output -

Sum of Integers :100
Sum of Doubles :101.85

Varargs And Ambiguity -

While overloading varargs methods some unexpected errors may occur. For example in the above program if we call the sum function without any arguments i.e. t1.sum();
In this case call to both the overloaded method is valid because any of them can run without any parameters. So compiler will be confused which method to call, So such conditions may lead to error.
Another case of ambiguity may be as follows -
public int sum(int x, int...a)
{
  // method body
}

public int sum(int... a)
{
  // method body
}

        
Here we have two overloaded methods, now suppose we call the sum method by passing only one integer parameter i.e. sum(10). In this case also call to both the methods is equally valid and this leads to ambiguity.