2

Could anyone please explain why this code throws an ambiguous overload error, surely the Integer method is more specific and applicable?

Thanks,

Ned

package object_orientation;
public class Ambiguous {
    //ambiguous error compiler unsure whether boxing is needed or not
    static void overload(Integer... d){
        System.out.println("Integer");
    }

    static void overload(long... d){
        System.out.println("Long");
    }

    public static void main(String a[]){
        int i = 1;
        overload(i);
    }
}
4

1 回答 1

3

These concepts in Java should help, Boxing + Widening is allowed, but not Widening + Boxing.

These rules of Widening, Boxing and Vararg should help:

  1. Primitive Widening > Boxing > Varargs.
  2. Widening and Boxing (WB) not allowed.
  3. Boxing and Widening (BW) allowed.
  4. While overloading, Widening + vararg and Boxing + vararg can only be used in a mutually exclusive manner
  5. Widening between wrapper classes not allowed.
  6. Widening+varArgs & Boxing+varargs are individually allowed (but not allowed in overloaded version of method).
  7. Boxing+Widening is preferred over Boxing+Varargs.

Hope this helps.

于 2013-08-19T13:12:10.673 回答