Why is it printing a line, not an object?
The java compiler selects a method with the most specific or least common argument. Since Object is the superclass of all classes (including String ), the String class is selected.
Why does a compilation error occur when adding a third method?
Since String and StringBuilder lower than Object , the compiler will find the call ambiguous, since both String and StringBuilder can accept null , the compiler cannot determine which method to call, therefore, you get an error at compile time.
If you try to do the same with IOException and FileNotFoundException instead of String and StringBuilder , you will find that FileNotFoundException because it is less general.
source share