VarArgs with Multiple Parameters

The following example illustrates varargs with multiple parameters.

class MultipleParam
{
public static void main(String args[])
{
sum("1+2+4+5=",1,2,4,5);
sum("1+2=",1,2);
int a[]={1,8151191,817};
sum("1+8151191+817=",a);
}

public static void sum(String s,int... a)
{
int sum=0;
for(int i=0;i<a.length;i++)
{
sum+=a[i];
}
System.out.println(s+sum);
}
}

Output



1+2+4+5=12
1+2=3
1+8151191+817=8152009

sum(String s,int... a): When declaring vararg method with multiple parameters the vararg parameter must succeed after normal parameters.


Posted by at on

Tags: Variable Arguments,