Method Overloading for null argument

前端 未结 7 720
悲哀的现实
悲哀的现实 2020-11-21 13:48

I have added three methods with parameters:

public static  void doSomething(Object obj) {
    System.out.println(\"Object called\");
}

public static  void d         


        
7条回答
  •  广开言路
    2020-11-21 14:19

    Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.

提交回复
热议问题