Method Overloading For Null Argument

Method Overloading for null argument

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.

Method overloading and passing null

Why it prints String and not Object?

The java compiler picks the method with the most specific or least generic argument. Since Object is the Superclass of all classes (including String), String class is picked.

Why is there compilation error on adding third method?

Since String and StringBuilder are below Object, the compiler will find the call ambiguous since both String and StringBuilder can accept null, the compiler fails to determine which method to call hence you get error during compilation.

If you try the same thing with IOException and FileNotFoundException instead of String and StringBuilder, you will find out that FileNotFoundException is picked since it is least generic.

Method Overloading for Null Argument by passing 0 and null

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

Object, double[] can both take null as a valid value. Therefore both are suitable.

Object is the super-type of double[] and therefore more specific than just Object. So thats the reason why it prints "Double array argument method." when you pass null to the function

For the ohter Question:
As already explained in the comments, when you pass 0 which is a primitive int, will be boxed automatically into an Integer which have Object as super-type, so it prints "Object o argument method."

You can find more info in Method Overloading for null argument

Using null in overloaded methods in Java

If you were asked what is more specialized "String" or "Object", what would you say? Evidently "String", right?

If you were asked: what is more specialized "String" or "Integer"? There is no answer, they are both orthogonal specializations of an object, how can you choose between them? Then you must be explicit regarding which one you want. For instance by casting your null reference:

question.method((String)null)

When you use primitive types you do not have that problem because "null" is a reference type and cannot conflict with primitive types. But when you use reference types "null" could refer to either String or Integer (since null can be cast to any reference type).

See the answer in the other question that I posted in the comments above for further and deeper details and even a few quotes from the JLS.

Is it a good practice to call to an overloaded method with null params?

The overloaded method with fewer parameters that just call the other method with more parameters, is a common practice in Java, and is the Java way of implementing "default" parameters.

Note that the default value doesn't have to be null, it can be any value.

Normally when calling a method with multiple parameters, the values passed in will give a clue to the nature of the parameter. Parameter constants like null, false, true, or 0 doesn't give a clue to the parameters meaning, which makes the code is less readable.

Usually a call with fewer parameter is more obvious, so overloading with "default" parameters is preferable to only one method with a lot of constant parameters.

How is an overloaded method chosen when a parameter is the literal null value?

Is null a String variable pointing to nothing ?

A null reference can be converted to an expression of any class type. So in the case of String, this is fine:

String x = null;

The String overload here is chosen because the Java compiler picks the most specific overload, as per section 15.12.2.5 of the JLS. In particular:

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.

In your second case, both methods are still applicable, but neither String nor StringBuffer is more specific than the other, therefore neither method is more specific than the other, hence the compiler error.

Passing null as argument in overloaded methods?

You need to cast null to the type you want so that the compiler can resolve the ambiguity.

For example:

PersonFactory.get("John", "Doe", (String) null);

or

PersonFactory.get("John", "Doe", (Integer) null);


Related Topics



Leave a reply



Submit