Java will always try to use the most appropriate version of the available method (see JLS ยง15.12.2 ).
Object , char[] and Integer can be null as a valid value. Therefore, all 3 versions are applicable, so Java will have to find the most specific.
Since Object is a supertype of char[] , the version of the array is more specific than the Object version. Therefore, if only these two methods exist, the version of char[] will be selected.
If the char[] and Integer versions are available, then both of them are more specific than Object , but neither is more specific than the other, so Java cannot decide which one can be called. In this case, you will have to explicitly indicate which one you want to call by returning an argument to the appropriate type.
Note that in practice this problem is much less common than one might think. The reason for this is that this only happens when you explicitly call a method with null or with a variable of a rather non-specific type (for example, Object ).
On the contrary, the following call will be absolutely unambiguous:
char[] x = null; doSomething(x);
Although you still pass null , Java knows exactly which method to call, since it will take into account the type of variable.
Joachim Sauer Mar 08 2018-11-11T00: 00Z
source share