Java Variable Arguments Example
lass VarArgs
{
static void vaTest(String msg, int… v)
{
System.out.println(msg +v.length+ ” Contents:”);
for(int x : v)
System.out.print(x+ ” “);
System.out.println();
}
public static void main(String args[])
{
vaTest(”One vaarg:”, 10);
vaTest(”Three varrgs:”,1,2,3);
vaTest(”No varrgs”);
}
}
Variable arguments or in short VarArgs introduced in J2SE1.5. This feature is very helpful for developers to declare as much as parameters for given arguments.
Below is the sample code for Variable Arguments to understand better
class VarArgs
{
static void vaTest(String msg, int… v)
{
System.out.println(msg +v.length+ ” Contents:”);
for(int x : v)
System.out.print(x+ ” “);
System.out.println();
}
public static void main(String args[])
{
vaTest(”One varargs:”, 10);
vaTest(”Three vararrgs:”,1,2,3);
vaTest(”No vararrgs”);
}
}