A)
->for a method if we declare a variable argument type as parameter, any no.of arguments we can supply during that method class. This concept is known as var arg concept.
-> var arg type parameter is created as fallows
datatype … variablename
class varargtest {
static int add (int ...a)
{
int sum=0;
for(int i=0;i<a.length;i++)
{
sum=sum+a[i];
}
return sum;
}
public static void main(String[] args) {
System.out.println("the sum of 2 numbers : "+add(10,70));
System.out.println("the sum of 2 numbers : "+add(110,60,40));
}
}
Output:
the sum of 2 numbers : 80
the sum of 2 numbers : 80
the sum of 2 numbers : 210
->A method can’t have more than one variable arguments type parameters.
->If a method has normal parameters and var arg parameters, var arg type should be the last parameters.
Comments
Post a Comment