Sometimes there will be a requirement in which
methods have to take variable number of arguments. In JDK 5 a feature is
introduced which helps to create this kind of methods (that take variable
number of arguments) easily. This new feature introduced in JDK 5 is called
Varargs or Variable-length arguments. A method that takes variable number
arguments is called a variable argument method or simply varargs method.
The varargs method is implemented
using the following general syntax:
type MethodName(type…variableName)
{
//body of
method
}
For example see the below code
fragment:
int VarargMethod (int … v)
{
//body of method
}
Points to remember while implementing
Varargs method:
1. The Varargs methods can be
overloaded. When implementing varargs method with multiple parameter then you
should always place variable parameter at the end of the parameter list. For
example see the below code fragment.
double VarargMethod (int
a, double b, int …
v)
{
//body of method
}
2. There must be only one variable
argument parameter in a method. More than one varargs parameter is not allowed
in any method.
The below example program shows
implementation of Varags methods.
package sample.example1627;
public class SampleVarargs
{
int varArg(int...v)
{
int sum=0;
for(int x:v)
{
System.out.println("element of vararg:
"+x);
sum+=x;
}
return sum;
}
}
package sample.example1627;
public class SampleVarargsDemo
{
public static void main(String[] args)
{
int sum;
SampleVarargs vararg1=new SampleVarargs();
sum=vararg1.varArg();
System.out.println("sum1 "+sum);
sum=vararg1.varArg(1,2,3);
System.out.println("sum2 "+sum);
sum=vararg1.varArg(10,20);
System.out.println("sum3 "+sum);
}
}
Output:
sum1 0
element of vararg: 1
element of vararg: 2
element of vararg: 3
sum2 6
element of vararg: 10
element of vararg: 20
sum3 30
The example program below shows varargs method
overloading
package sample.example1627;
public class SampleVarargOverloading
{
double varArgMethod(int...v)
{
int mul=1;
for(int a:v)
{
System.out.println("argument passed
"+a);
mul*=a;
}
return mul;
}
void varArgMethod(int a,char b,double...v)
{
System.out.println("arguments passed
by normal parameter "+a+" & "+b);
for(double x:v)
{
System.out.println("argument passed by
vararg "+x);
}
}
}
package sample.example1627;
public class
SampleVarargOverloadingDemo
{
public static void main(String[] args)
{
SampleVarargOverloading
varargOv1=new
SampleVarargOverloading();
double mul;
System.out.println("first vararg
method version");
mul=varargOv1.varArgMethod(1,2,3,4,5);
System.out.println("multiplication
return result "+mul);
System.out.println();
System.out.println("second vararg
method version");
varargOv1.varArgMethod(20, 'A', 1.6,3.5,2.55,5.66,8.99);
}
}
Output:
first vararg method version
argument passed 1
argument passed 2
argument passed 3
argument passed 4
argument passed 5
multiplication return result 120.0
second vararg method version
arguments passed by normal parameter 20 & A
argument passed by vararg 1.6
argument passed by vararg 3.5
argument passed by vararg 2.55
argument passed by vararg 5.66
argument passed by vararg 8.99