java - overloading with both widening and boxing -


public void add(long... x){} public void add(integer... x){}  add(2); 

this produces error...why overlaoding not performed both widening , boxing?

but overloading without vararg works fine

public void add(long x){}     public void add(integer x){}      add(2); 

here add(long x) executed widening beats boxing...why not same concept var arguments

java compiler performs 3 attempts choose appropriate method overload (jls §15.12.2.1):

  • phase 1: identify matching arity methods applicable subtyping
    (possible boxing conversions , methods varargs ignored)

  • phase 2: identify matching arity methods applicable method invocation conversion
    (takes boxing conversion in account, ignores methods varargs)

  • phase 3: identify applicable variable arity methods
    (examines possibilities)

so, examples works follows:

  • without varargs: add(long x) identified applicable method on 1st phase (this method applicable subtyping since int subtype of long, §jls 4.10.1), following phases not executed.

  • with varargs: overload resoltion algorithm goes phase 3, both methods identified applicable, , compiler can't choose specific method of them (choosing specific method yet complex algorithm), therefore reports ambiguity.

see also:


Comments

Popular posts from this blog

javascript - Enclosure Memory Copies -

php - Replacing tags in braces, even nested tags, with regex -