When compiling a method call, the compiler searches for a matching method in this order:
- Try to find a method without autoboxing/unboxing or varargs.
- If no method is found, see if a method matches using autoboxing/unboxing.
- If still no method is found, see if a method matches using both varargs and autoboxing/unboxing.
In your case, you are in step (3) here. Since unboxing is allowed, both methods are applicable.
The compiler then tries to find the one that is most specific. One method is more specific than another if its parameter types are all equally or more specific (for example, String
is more specific than Object
, and int
is more specific than long
).
But between int
and Integer
, neither is more specific; the compiler relies on testing (1) before (2) above to get the correct method in the case of, say, foo(int)
and foo(Integer)
.
So, there are two applicable methods and neither is more specific, hence the ambiguity error.