Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does null have Object type?

Tags:

java

Why does this output: "inside String argument method"? Isn't null "Object" type?

class A {
    void printVal(String obj) {
        System.out.println("inside String argument method");
    }

    void printVal(Object obj) {
        System.out.println("inside Object argument method");
    }

    public static void main(String[] args) {
        A a = new A();
        a.printVal(null);
    }
}
like image 977
brainydexter Avatar asked Feb 04 '23 01:02

brainydexter


2 Answers

The most specific matching method will be called. More information here:

  • Which overload will get selected for null in Java?
like image 60
Gunslinger47 Avatar answered Feb 05 '23 14:02

Gunslinger47


Yes, but null is also String type and all other types, so it selects the most specific method to call.

like image 33
amara Avatar answered Feb 05 '23 15:02

amara