As per JLS (§15.12.2)
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.
This ensures that a method is never chosen through variable arity
method invocation if it is applicable through fixed arity method
invocation.
The third phase (§15.12.2.4) allows overloading to be combined
with variable arity methods, boxing, and unboxing.
In our case third phase is applied:
All these steps below tried (even if a match found, next step is tried further!!!)
- First argument (primitive) widening (see if widened type is same as vararg type)
- First argument boxing (see if boxed type is same as vararg type)
- First argument boxing and then widening reference (not sure)?
Plus, for every step above, same 3 steps are performed for the second argument (making it 3*3 = 9 different paths-attempts).
If more than one methods are found, most specific is chosen: f(Integer) is more specific than f(Number) for f(2) call
. Usually most specific means that arguments are in a hierarchy.
In our case both methods are chosen and the arguments are not in any hierarchy to find a more specific method. This causes ambiguity.