Method oveloading - Object vs Object vararg

后端 未结 2 1780
孤独总比滥情好
孤独总比滥情好 2021-01-19 06:24

See the code below:

// 1st method
private static void method(Object o){
    System.out.println(\"object method\");
}
// 2nd method
private static void method         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-01-19 07:21

    Because Object... is essentially Object[] and since null is a valid Object[], it will match to the most specific one.

    If you had 3 methods with first having Object parameter, the second having SubClass param and the last having SubSubClass param, the last one would be chosen.

    Whereas if you add a method with a String parameter to your original code, you will get a compile time error, since there is no longer a single most specific match for null.

提交回复
热议问题