TechieDrill Your World Of Technical Tutorials

Java Variable Arguments Example

11.20.2009 · Posted in Java
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”);

}

}

Leave a Reply

You must be logged in to post a comment.