Java – how does null work in this code?

See English answers > method overloading and choosing the most specific type 9

class Test1{
    public void doStuff(Object o){
    System.out.println("In Object");
}

    public void doStuff(String o){
        System.out.println("In String");
    }
}

public class TTest {
    public static void  main(String args[]){    
        Test1 t = new Test1();
        t.doStuff(null);
    }
}

Output:

Solution

Java always tries to use the most specific method version

Self summoning

t.doStuff(null);

Applicable to both methods

t.doStuff(Object o)
t.doStuff(String o)

Java will choose the most specific method description, namely

t.doStuff(String o)
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
分享
二维码
< <上一篇
下一篇>>