Why the constructor Array and Integer are ambiguous

I wrote this program in eclipse and eclipse complains that the constructor is ambiguous. I'm not sure why the java compiler calls them ambiguous.

public class Ambigious {

    public Ambigious(float[] a){
        System.out.println("array constructor");
    }


    public Ambigious(Integer a){
        System.out.println("Integer constructor");
    }

    public static void main(String[] args) {
        new Ambigious(null);
    }

}

But it is not

public class Ambigious {

    public Ambigious(Object a){
        System.out.println("object constructor");
    }

    public Ambigious(float[] a){
        System.out.println("array constructor");
    }

    public static void main(String[] args) {
        new Ambigious(null);
    }

}
+4
source share
2 answers

Problem 1:
The two float[]and Integerare objects, however nullcan be applied to both designers.

Since there is no suitable type compiler, it cannot decide which one should be used.

2:
Object Integer . , new Integer(1) , , Integer Object, .
null, Object Integer, , Integer.


, , null

new Ambigious((Integer)null);

-,

public Ambigious(int a){//primitive type

public Ambigious(Integer a){
+7

reference to Ambigious is ambiguous, both constructor Ambigious(float[]) in Ambigious and constructor Ambigious(Integer) in Ambigious match.

, , null.

+3

All Articles