Why and how does this java code compile?

See English answer > which overload will get selected for null in Java? three

public class Riddle {

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

    public static void hello(Object o) {
        System.out.println("Object");
    }


    public static void hello(String s) {
        System.out.println("String");
    }

}

Why code compilation? Isn't it empty?

For example, the following code will not compile because of fuzzy signatures

public class Riddle {

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

    public static void hello(Object o) {
        System.out.println("Object");
    }

    public static void hello(Integer o) {
        System.out.println("Integer");
    }

    public static void hello(String s) {
        System.out.println("String");
    }

}

Can someone explain why the first example can compile unambiguous errors?

Solution

In the second case, the compiler cannot compile between methods using integer and string, but in the first case, the compiler can calculate

reference resources: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls -15.12. two

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>