Method overload for null argument

I added three methods with parameters:

public static void doSomething(Object obj) { System.out.println("Object called"); } public static void doSomething(char[] obj) { System.out.println("Array called"); } public static void doSomething(Integer obj) { System.out.println("Integer called"); } 

When I call doSomething(null) , the compiler throws an error as ambiguous methods . Is this a problem because the Integer and char[] methods or the Integer and Object methods?

+96
java null oop method-overloading
Mar 08 2018-11-11T00:
source share
6 answers

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.

+161
Mar 08 2018-11-11T00:
source share

Each pair of these three methods is ambiguous in itself when called with an argument of null . Because each type of parameter is a reference type.

Listed below are three ways to call one specific method with a null value.

 doSomething( (Object) null); doSomething( (Integer) null); doSomething( (char[]) null); 

May I suggest eliminating this ambiguity if you are actually planning on calling these methods null arguments. Such a design offers errors in the future.

+35
Mar 08 2018-11-11T00:
source share

null is a valid value for any of the three types; therefore, the compiler cannot decide which function to use. Instead, use something like doSomething((Object)null) or doSomething((Integer)null) .

+4
Mar 08 2018-11-11T00:
source share

Each class in Java extends the Object class. Another Integer class also extends Object. Therefore, Object and Integer are treated as an instance of an object. Therefore, when you pass null as a parameter, and the compiler gets confused which method of the object is called, for example, with the Object parameter or the Integer parameter, since both of them are objects, and their reference may be null. But primitives in java do not extend Object.

+2
Mar 08 2018-11-11T00:
source share

I tried this, and when there is only one pair of overloaded methods, and one of them has an Object type, then the compiler will always choose a method with a more specific type. But when there is more than one particular type, the compiler throws an ambiguous method error.

Since this is a compile-time event, this can only happen when one intentionally passes null to this method. If this is done intentionally, then it is better to overload this method again without parameters or create another method altogether.

+1
Jul 30 '14 at 15:57
source share

there is ambiguity due to doSomething (char [] obj) and doSomething (Integer obj).

char [] and Integer are both the same for null, so they are ambiguous.

0
Nov 04 '17 at 0:41
source share



All Articles