Posts Tagged varargs
Demonstrates The overloading Varargs methods.
class VarArgs
{public void Test(int ... v)
{System.out.print("No. of args "+ v.length +"contents ");
for(int i : v)
{System.out.print(i+" ");}
System.out.println();
}
public void Test(boolean ... v)
{System.out.print("No. of args "+ v.length+"contents ");
for(boolean i: v)
{System.out.print(i+ " ");}
System.out.println();
}
public void Test(String str, int ... v)
{System.out.print(str+ v.length+"contents ");
for(boolean i: v)
{System.out.print(i+ " ");}
System.out.println();
}
public static void main(String a[])
{ VarArgs va = nwe VarArgs();
va.Test("Testing me:", 10, 20);
va.Test(11,12,13);
va.Test(true, false, false);
//va.Test(); // Error ambiguity between boolean and integer function
}
}
Demonstrates the VARARGS feature of java for variable arguments along with normal parameters.
Here we demonstrate how we can pass normal parameter along with varible arguments.
class VarExample
{ public void Test(String str, int ... arr)
{System.out.print(str + arr.length + "contents ");
for(int i : arr){
System.out.print(i+" ");}
System.out.println();
}
public static void main(String a[])
{VarExample ve =new VarExample();
ve.Test("First argument",1);
ve.Test("Second argument", 11, 12,13);
ve.Test(" No variable arguments ");
}
}


Recent Comments