How var-arg method is implemented internally?
class VarArgMethod2
{
public static void main(String[] args)
{
sum(10);
sum(10,20);
sum(10,20,30);
}
public static void sum(int...x)
{
int total=0;
for(int i:x)
total+=i;
System.out.println("Sum = "+total);
}
}
/*
Output:- 10
30
60
Explanation:- Internally var-arg parameter implemented by 1-Dimensional array. Hence within
var-arg method we can differentiate arguments by using index.
*/
{
public static void main(String[] args)
{
sum(10);
sum(10,20);
sum(10,20,30);
}
public static void sum(int...x)
{
int total=0;
for(int i:x)
total+=i;
System.out.println("Sum = "+total);
}
}
/*
Output:- 10
30
60
Explanation:- Internally var-arg parameter implemented by 1-Dimensional array. Hence within
var-arg method we can differentiate arguments by using index.
*/
0 comments: