I think that would be a stupid question, but I cannot understand why this is so.
the code
public class OverloadingTest {
public static int sum(int ...a){
int sum=0;
for(int i : a){
sum += i;
}
System.out.print("this is variant args//");
return sum;
}
public static double sum(double a, double b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(sum(1.5, 2.5));
System.out.println(sum(10, 20));
System.out.println(sum(10, 20,30));
}
}
Expected results;
4.0
this is variant args
this is variant args
actual results in the console:
4.0
30.0
this is variant args
I can’t understand why the results are sum(10, 20)30.0 and not 30 variable arguments.
source
share