I think this would be a stupid question a little bit, but I can not sure why it is.
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));
}
}
results I expected;
4.0
this is variant args//30
this is variant args//60
actual results in console:
4.0 30.0 this is variant args//60
I can not sure why results of sum(10, 20)
30.0, not 30 of variable argument.
The answer is in JLS section 15.12.2. Basically, the compiler tries to find any applicable method without having to expand varargs, only using varargs if it has to:
The remainder of the process is split into three phases, to ensure compatibility with versions of the Java programming language prior to Java SE 5.0. The phases are:
The first phase (§15.12.2.2) performs overload resolution without permitting boxing or unboxing conversion, or the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the second phase. [...]
The second phase (§15.12.2.3) performs overload resolution while allowing boxing and unboxing, but still precludes the use of variable arity method invocation. If no applicable method is found during this phase then processing continues to the third phase. [...]
The third phase (§15.12.2.4) allows overloading to be combined with variable arity methods, boxing, and unboxing.
In this case, the first phase does find a match, as sum(double, double)
is applicable for the call sum(10, 20)
due to the implicit conversion from int
to double
.
See more on this question at Stackoverflow