3
class MyClass{
    static void aut(int i) {}
    static void aut(Integer i) {}

    static void vararg(int... ia) {}
    static void vararg(Integer... ia) {}

    public static void main(String args[]) {
        aut(1); // compiles successfully 
        vararg(1); // The method vararg(int[]) is ambiguous for the type MyClass
    }
}

I am not able to understand why vararg(1) is throwing an error.

1 Answers1

6

Most specific method should be choosen and that should the method with single int parameter and not varargs.

Looks like the bug of Java not completely resolved although they marked it as resolved.

JDK-6886431 : Handling of "most specific" varargs method inconsistent with JLS

A DESCRIPTION OF THE PROBLEM :

When invoking a method with the following overloaded signatures, I expect an ambiguity error (assuming the arguments are compatible with both):

int f(Object... args);
int f(int... args);

javac treats the second as more specific than the first. This behavior is sensible (I prefer it), but is inconsistent with the JLS (15.12.2).

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307