The compiler will consider all possible overloads of the method, which may correspond to the parameters that you pass. If one of them is more specific than all the others, then it will be chosen, it will be considered ambiguous, if there is no single most specific overload.
In your example, there are two possible overloads, method1(Object) and method1(String) . Since String more specific than Object , there will be no ambiguity, and the String parameter will be selected. If there was a third overload, such as method1(Integer) , then there is no longer much choice for calling method1(null) , and the compiler will generate an error.
source share