This doesn't compile. The problem is that, in the second example, two methods are "maximally specific" and the method call is considered ambiguous.
You can find more details about exactly how a method is chosen over another by looking at the JLS,
§15.12.2.2 Choose the Most Specific Method. This is the part that explains why the method that takes an Integer or a
String over one that takes an Object:
The informal intuition is that one method declaration is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
The precise definition is as follows. Let m be a name and suppose that there are two declarations of methods named m, each having n parameters. Suppose that one declaration appears within a class or interface T and that the types of the parameters are T1, . . . , Tn; suppose moreover that the other declaration appears within a class or interface U and that the types of the parameters are U1, . . . , Un. Then the method m declared in T is more specific than the method m declared in U if and only if both of the following are true:
- T can be converted to U by method invocation conversion.
- Tj can be converted to Uj by method invocation conversion, for all j from 1 to n.
If you still have questions about that, please ask. This topic can be rather confusing.
I hope that helps,
Corey