Method overload selection with null

后端 未结 2 1797
长情又很酷
长情又很酷 2021-01-13 22:13

Given this code:

class Overloading
extends Object
{

static public void target(Object val, String chk) { System.out.println(\"Object[\"+val+\"] :: Should be          


        
2条回答
  •  梦毁少年i
    2021-01-13 23:08

    Java has always worked the same way: the "most specific" applicable overload is always chosen. Since String is a subclass of Object, it is "more specific", and the String overload is chosen. If the overloads were for, say String and Integer, and you tried to pass null, then you would indeed get a compile-time ambiguity error, since they are both at the same level of the same inheritance hierarchy.

提交回复
热议问题