Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Java compiler performs three attempts to choose an appropriate method overload (<a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.12.2.1" rel="nofollow">JLS §15.12.2.1</a>):</p> <ul> <li><p>Phase 1: Identify Matching Arity Methods Applicable by Subtyping<br> (possible boxing conversions and methods with varargs are ignored)</p></li> <li><p>Phase 2: Identify Matching Arity Methods Applicable by Method Invocation Conversion<br> (takes boxing conversion in account, but ignores methods with varargs)</p></li> <li><p>Phase 3: Identify Applicable Variable Arity Methods<br> (examines all possibilities)</p></li> </ul> <p>So, with your examples it works as follows:</p> <ul> <li><p>Without varargs: <code>add(long x)</code> is identified as the only applicable method on the 1st phase (this method is applicable by subtyping since <code>int</code> is a subtype of <code>long</code>, <a href="http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.10.1" rel="nofollow">§JLS 4.10.1</a>), so that following phases are not executed.</p></li> <li><p>With varargs: overload resoltion algorithm goes to phase 3, where both methods are identified as applicable, and compiler can't choose the most specific method of them (choosing the most specific method is yet another complex algorithm), therefore it reports ambiguity.</p></li> </ul> <p><strong>See also:</strong></p> <ul> <li><a href="http://docs.oracle.com/javase/specs/jls/se7/html/index.html" rel="nofollow">The Java Language Specification, Seventh Edition</a></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload