Hi Eduardo,
Thanks for posting this question.
The compiler determines which method to call by going thru' 3 phases.
Extracted from
JLS3 Section 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.
The third phase (�15.12.2.4) allows overloading to be combined with variable
arity methods, boxing and unboxing. ------------
Here's what I think based on the rules above:
//A
method(1); // OK Without boxing/unboxing and arity methods, the compiler can find an exact method in phase 1, i.e. method(int i).
//B
method(new Integer(1),new Integer(1)); // Compilation error It isn't able to pass phase 1 since there's no method(Integer x, Integer y). Not for phase 2 too since there's no method(int x, int y), method(int x, Integer y) or method(Integer x, y). For phase 3, any of the following signature combination is possible, hence result in ambiguity during compilation:
Same for D, E, F.
//C
method(new Integer(1)+1);
//G
method(1+new Integer(1)); According to JLS3 5.6.2 Binary Numeric Promotion, the result type of the two operands will be int.
The following program is legal:
After converting to int for both C and G, the compiler can find a matching method in phase 1, i.e. method(int i). Hence compilation passed.
What do you think?
[ January 26, 2005: Message edited by: Joyce Lee ]