Advertisement
public void meta(int... i)
//Java - Example of varargs
class A
{
public void meth(int... a)
{
System.out.println("Method meth is called");
}
public static void main(String... ar)
{
A ob= new A();
ob.meth(); //Caling meth() with zero argument
ob.meth(1); //Caling meth() with one argument
ob.meth(1,2,3); //Caling meth() with multiple arguments
}
}
Method meth is called
Method meth is called
Method meth is called
Advertisement
//Java - Example of varargs
class A
{
public void display(char c, int a, long... l)
{
System.out.println("The value of a : " + a);
System.out.println("The value of c : " + ch);
System.out.println("The value of l : " + l);
}
public void display(char c, int... a, long l)
{
System.out.println("The value of a : " + a);
System.out.println("The value of c : " + ch);
System.out.println("The value of l : " + l);
}
}
var.java:12: error: ')' expected
public void display(char c, int... a, long l)
^
var.java:12: error: ';' expected
public void display(char c, int... a, long l)
^
2 errors
Advertisement
//Java - Example of varargs
class A
{
public void show(int... a, char... ch)
{
System.out.println("The value of a :" + a);
System.out.println("The value of c :" + ch);
}
}
var.java:4: error: ')' expected
public void show(int... a, char... ch)
^
var.java:4: error: <identifier> expected
public void show(int... a, char... ch)
^
var.java:4: error: <identifier> expected
public void show(int... a, char... ch)
^
3 errors
//Java - Example of varargs
class A
{
public void meth(int... a)
{
if(a.length==0)
System.out.println("number of parameters passed : "+ a.length);
if(a.length>0)
{
System.out.println("number of parameters passed : "+ a.length);
for(int i=0;i<a.length;i++)
System.out.println(a[i]);
}
}
public static void main(String... ar)
{
A ob= new A();
ob.meth(); //calling method() with zero parameter
ob.meth(1); //calling method() with one parameter
ob.meth(1,2,3); //calling method() with more than one parameter
}
}
number of parameters passed : 0
number of parameters passed : 1
1
number of parameters passed : 3
1
2
3
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement