SEM-III
KNS INSTITUTE OF TECHNLOGY
JAVA CH-2
DEPARTMENT OF MCA
2.13 Varargs: Variable-Length Arguments :
o A method that takes a variable number of arguments is called varargs method.
o Ex: system.out.prinln(); method takes variable number of arguments.
o Previously before introducing this concept people use to go with array concept:
Example program showing use of variable length arguments using Array:
// Use an array to pass a variable number of
// arguments to a method. This is the old-style
// approach to variable-length arguments.
class PassArray
{
static void vaTest(int v[])
{
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[])
{
// Notice how an array must be created to
// hold the arguments.
int n1[] = { 10 };
int n2[] = { 1, 2, 3 };
int n3[] = { };
vaTest(n1); // 1 arg
vaTest(n2); // 3 args
vaTest(n3); // no args
}
}
output of the program is:
Number of args: 1 Contents: 10
Number of args: 3 Contents: 1 2 3
Number of args: 0 Contents:
Example program showing use of variable length in JAVA:
// Demonstrate variable-length arguments.
class VarArgs {
// vaTest() now uses a vararg.
static void vaTest(int ... v)
{
System.out.print("Number of args: " + v.length + " Contents: ");
for(int x : v)
System.out.print(x + " ");
System.out.println();
}
Lecturer: Syed Khutubuddin Ahmed
public static void main(String args[])
{
// Notice how vaTest() can be called
//with a variable number of arguments.
vaTest(10); // 1 arg
vaTest(1, 2, 3); // 3 args
vaTest(); // no args
}
}
Note: Loop is called: for each loop
15
SEM-III
KNS INSTITUTE OF TECHNLOGY
JAVA CH-2
DEPARTMENT OF MCA
o Remember, the varargs parameter must be last. For example, the following declaration is
incorrect:
o int doIt(int a, int b, double c, int ... vals, boolean stopFlag) { // Error!
o There is one more restriction to be aware of: there must be only one varargs parameter.
For example, this declaration is also invalid:
o int doIt(int a, int b, double c, int ... vals, double ... morevals) { // Error!
o The attempt to declare the second varargs parameter is illegal.
Overloading Vararg Methods:
You can have multiple methods with same name but different type of variable arguments:
Ex:
Static void add(int v)
{
}
static void add(float v)
{
}
o A varargs method can also be overloaded by a non-varargs method.
o For example, vaTest(int x) is a valid overload of vaTest( ) in the foregoing program.
This version is invoked only when one int argument is present.
o When two or more int arguments are passed, the varargs version vaTest(intv) is used.
Varargs and Ambiguity:
static void vaTest(int ... v)
{
}
static void vaTest(boolean ... v)
{
}
Calling of function
vaTest(1, 2, 3); // OK
vaTest(true, false, false); // OK
vaTest(); // Error: Ambiguous!
which function to call, is it Boolean or int
static void vaTest(int ... v) { // ...
static void vaTest(int n, int ... v) { // ...
vaTest(1)// ambiguity
Lecturer: Syed Khutubuddin Ahmed
16