Variable-Length Arguments

Java provides us to give variable number of arguments which is called as varargs short form of variable length arguments.

The method that takes variable number of arguments is called varargs method.

There are some situations where we have to give some necessary data along with default data.

It is done in two ways:

  1. we can use an array which holds the arguments.
  2. By using three periods (...).

1.

//example for using array for varargs
public class PassArray 
{
  static void vaTest(int v[]) //array v holds the varargs
  {
 System.out.print("Number of args: " + v.length +" Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println();
 }

 public static void main(String args[])
 {

 //arrays to hold the different number of arguments.
 int n1[] = { 10 };
 int n2[] = { 1, 2, 3 };
 int n3[] = { };
 vaTest(n1); // 1 arg
 vaTest(n2); // 3 args
 vaTest(n3); // no args
 }
}

Output:

Number of args: 1 Contents: 10
Number of args: 3 Contents: 1 2 3
Number of args: 0 Contents:

2.

A variable length argument is specified by three dot operators. (...)

//example for using (...)
public class VarArg1
{
static void vaTest(int ...v) //v holds the varargs
{
System.out.print("Number of args: " + v.length +" Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}

public static void main(String args[])
{

//arrays to hold the different number of arguments.
int n1[] = { 10 };
int n2[] = { 1, 2, 3 };
int n3[] = { };
vaTest(n1); // 1 arg
vaTest(n2); // 3 args
vaTest(n3); // no args
}
}

Output:

Number of args: 1 Contents: 10
Number of args: 3 Contents: 1 2 3
Number of args: 0 Contents:

The varargs parameter must be last when we are passing along with another variables

int test(int a, double d,int ...v)

int test(int a, int ...v, double d) //error

There must be onlyonevarargs parameter.

int test(int a, int ...v, double ...dv) //error

//var arguments as a last variable in method definition 
public class VarArgs2 
   {
 static void vaTest(String msg, int ... v) //one argument followed by variable length argument
 {
 System.out.print(msg + v.length + " Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println();
 }
 public static void main(String args[])
 {
 vaTest("One vararg: ", 10);
 vaTest("Three varargs: ", 1, 2, 3);
 vaTest("No varargs: ");
 }
}

Output:

One vararg: 1 Contents: 10
Three varargs: 3 Contents: 1 2 3
No varargs: 0 Contents:

Overloading Vararg Methods

we can overload vararg methods by different list of parameters it takes.

//example for varargs overloading
public class VarArgsOverloading
{
 static void vaTest(int ... v) //for integer type values
 {
 System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println();
 }

 static void vaTest(boolean ... v) //for boolean type values
 {
 System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");
 for(boolean x : v)
 System.out.print(x + " ");
 System.out.println();
 }

 static void vaTest(String msg, int ... v) //for int type of values followed by string
 {
 System.out.print("vaTest(String, int ...): " + msg + v.length + " Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println();
 }

 public static void main(String args[])
 {
 vaTest(1, 2, 3);
 vaTest("Testing: ", 10, 20);
 vaTest(true, false, false);
 }
}

Output:

vaTest(int ...): Number of args: 3 Contents: 1 2 3
vaTest(String, int ...): Testing: 2 Contents: 10 20
vaTest(boolean ...) Number of args: 3 Contents: true false false

Varargs and Ambiguity

Sometimes overloading of varargs methods leads to ambiguity if there is no proper match for the arguments in the method call.

//example for varargs ambiguity
public class VarArgsOverloading
{
 static void vaTest(int ... v) //for integer type values
 {
 System.out.print("vaTest(int ...): " + "Number of args: " + v.length + " Contents: ");
 for(int x : v)
 System.out.print(x + " ");
 System.out.println();
 }

 static void vaTest(boolean ... v) //for boolean type values
 {
 System.out.print("vaTest(boolean ...) " + "Number of args: " + v.length + " Contents: ");
 for(boolean x : v)
 System.out.print(x + " ");
 System.out.println();
 }



 public static void main(String args[])
 {
 vaTest(1, 2, 3);
 vaTest(true, false, false);
   varTest(); //it leads ambiguity throws error
 }
}

results matching ""

    No results matching ""