1

Why I get "ambiguous" compile error overloading methods like void f(int... arg) and void f(Integer... arg) ?

I know that according to JLS argument resolution is done in three steps (proceeding to next step only if previous step yielded no match):

  1. primitive (and its widening if exact match fails)
  2. boxing (and reference widening if exact match fails)
  3. varargs (doing steps 1 and 2 but for varargs)

Question 1: So when it comes to third step (varargs), it tries both to convert to vararg using steps 1 and step 2 (now it tries both equally, with no precedence) and if there can be 2 signatures it is ambiguous. I thought vararg checking should stop at int... and never try boxing after having a match at step 1.

Question 2: void f(int arg) and void f(long arg) never give "umbiguous" error, so within one step (step 1 here) "exact match" and "widening" never compete - having exact match compiler never tries to do widening (unlkike third vararg step when after matching primitive vararg it also tries boxing and matching boxed vararg)

Nicholas K
  • 15,148
  • 7
  • 31
  • 57
caasdads
  • 409
  • 4
  • 12

0 Answers0