Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method Overloading for null argument

People also ask

What happens if we pass null in overloaded method?

That means they accept null values. When we pass a null value to the method1 the compiler gets confused which method it has to select, as both are accepting the null. This compile time error wouldn't happen unless we intentionally pass null value.

How do you pass a null argument in Java?

The following are the three ways to call one specific method of yours with null. doSomething( (Object) null); doSomething( (Integer) null); doSomething( (char[]) null);

Can we pass null as argument for string in Java?

It is a value of the reference variable. The access to a null reference generates a NullPointerException. It is not allowed to pass null as a value to call the methods that contain any primitive data type.

Why method overloading is not possible with?

In java, method overloading is not possible by changing the return type of the method only because of ambiguity.


Java will always try to use the most specific applicable version of a method that's available (see JLS §15.12.2).

Object, char[] and Integer can all take null as a valid value. Therefore all 3 version are applicable, so Java will have to find the most specific one.

Since Object is the super-type of char[], the array version is more specific than the Object-version. So if only those two methods exist, the char[] version will be chosen.

When both the char[] and Integer versions are available, then both of them are more specific than Object but none is more specific than the other, so Java can't decide which one to call. In this case you'll have to explicitly mention which one you want to call by casting the argument to the appropriate type.

Note that in practice this problem occurs far more seldom than one might think. The reason for this is that it only happens when you're explicitly calling a method with null or with a variable of a rather un-specific type (such as Object).

On the contrary, the following invocation would be perfectly unambiguous:

char[] x = null;
doSomething(x);

Although you're still passing the value null, Java knows exactly which method to call, since it will take the type of the variable into account.


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

The following are the three ways to call one specific method of yours with null.

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

May I suggest to remove this ambiguity if you actually plan to call these methods with null arguments. Such a design invites errors in the future.


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


Every class in Java extends Object class.Even Integer class also extends Object. Hence both Object and Integer are considered as Object instance. So when you pass null as a parameter than compiler gets confused that which object method to call i.e. With parameter Object or parameter Integer since they both are object and their reference can be null. But the primitives in java does not extends Object.


I Have tried this and when there is exactly one pair of overloaded method and one of them has a parameter type Object then the compiler will always select the method with more specific type. But when there is more than one specific type, then 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 with no parameter or create another method altogether.