I just found out about a very interesting Java trick:
void method1(Integer... a){
}
So you can give this method as many integers as you want.>
To determine which method should be called, the compiler goes through the following list, as detailed in the JLS #5.3 and JLS #15.12.2:
method1(int a, int b)
method1(Integer... a)
In your case, the first point applies and method1(int, int)
is called.
(To be more precise, your method uses varags and has a lower priority than a simple boxing conversion. In other words, if there were a method1(Integer a, Integer b)
it would come before method1(Integer... a)
in the hierarchy)
Why is it so? A comment in 15.12.2 give a hint:
This guarantees that any calls that were valid in the Java programming language before Java SE 5.0 are not considered ambiguous as the result of the introduction of variable arity methods, implicit boxing and/or unboxing.