0

I have one class

public class HelloWorld {

    HelloWorld(Float[] a) {
    }

    HelloWorld(Double a) {
    }
}

In the main program I am doing this:

public static void main(String args[]) {
        HelloWorld helloWorld = new HelloWorld(null);
    }

And eclipse showing "The constructor HelloWorld(Float[]) is ambiguous" Why?

Rahul
  • 97
  • 1
  • 9
  • 1
    related http://stackoverflow.com/questions/30850695/why-java-prefers-to-call-double-constructor/30850736#30850736 – River Jun 15 '15 at 17:21
  • Can you try to explain what exactly is unclear for you? Both methods accept 'null'? How would you decide which one to call? – Tom Jun 15 '15 at 17:54

4 Answers4

10

The reason is that null can be considered any type. However, Float[] and Double aren't directly related. Float[] isn't a subtype of Double, and Double isn't a subtype of Float[]. The compiler sees two applicable methods, yet neither method is more specific than the other. In this case, the compiler generates an error because the method call is ambiguous; it doesn't know which method needs to be invoked.

Section 4.1 of the JLS states:

The null reference can always be assigned or cast to any reference type (§5.2, §5.3, §5.5).

In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

In addition, Section 15.12.2.5 of the JLS, about choosing the most specific method, states:

It is possible that no method is the most specific, because there are two or more methods that are maximally specific. In this case:

  • If all the maximally specific methods have override-equivalent signatures (§8.4.2), then:

    • If exactly one of the maximally specific methods is concrete (that is, non-abstract or default), it is the most specific method.

    • Otherwise, if all the maximally specific methods are abstract or default, and the signatures of all of the maximally specific methods have the same erasure (§4.6), then the most specific method is chosen arbitrarily among the subset of the maximally specific methods that have the most specific return type.

      In this case, the most specific method is considered to be abstract. Also, the most specific method is considered to throw a checked exception if and only if that exception or its erasure is declared in the throws clauses of each of the maximally specific methods.

  • Otherwise, the method invocation is ambiguous, and a compile-time error occurs.

rgettman
  • 176,041
  • 30
  • 275
  • 357
2

This error basically happens when you try to call a function and the compiler doesn't have enough information to determine which method to be called.

In your case, when you say new HelloWorld(null) it doesn't know whether null refers to Float[] or Double.

Vivek Sadh
  • 4,230
  • 3
  • 32
  • 49
1

When you pass null, your program is confused about which constructor is to be called. Because both constructors take one single parameter. And now it has two choices.

In general english, ambigious means you have more than one undecidable way to accomplish a task.

So, in programming, if ambiguity is encountered, the app crashes if ambiguity is not explicitly handles.

rpax
  • 4,468
  • 7
  • 33
  • 57
Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
0

There is no relation between Float[] and Double. That's the reason compiler getting ambiguity. Null can be interpreted to any reference in java.

Suppose if you have Float[] and Object then it will execute Float[] constructor. There is a relation between Object and FLoat. All wrapper classes are sub class to Object class. Float[] should be more specific type.

Reference:

Why does Java prefer to call double constructor?

Community
  • 1
  • 1