This is because you can pass an Object
or a String
. Since null
can fit in both, the compiler doesn't know which method to use, leading to compile error.
Methods definitions:
Instead, if you provide an Object
or a String
variable (even if it has null
value), the compiler would know which method to use.
EDIT: This is better explained in this answer. As to the internal link pointing to the Java specification, you can read it here, and this case would suit here:
The informal intuition is that one method is more specific than another **if any invocation handled by the first method could be passed on to the other one without a compile-time type error.
It is possible that no method is the most specific, because there are two or more methods that are maximally specific.