Which Overload Will Get Selected for Null in Java

Which overload will get selected for null in Java?

The most specific method will be called - in this case

showInputDialog(Component parent, Object message)

This generally comes under the "Determine Method Signature" step of overload resolution in the spec (15.12.2), and in particular "Choosing the Most Specific Method".

Without getting into the details (which you can read just as well in the spec as here), the introduction gives a good summary:

If more than one member method is both
accessible and applicable to a method
invocation, it is necessary to choose
one to provide the descriptor for the
run-time method dispatch. The Java
programming language uses the rule
that the most specific method is
chosen.

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.

How to do 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.

Which method will get invoked during overloading when null is passed

As explained by Rohit here,

That is because String class extends from Object and hence is more
specific to Object. So, compiler decides to invoke that method.
Remember, Compiler always chooses the most specific method to invoke.

If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.

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.

However, if you have two methods with parameter - String, and Integer,
then you would get ambiguity error for null, as compiler cannot decide
which one is more specific, as they are non-covariant types.

As described in Section 15.12.5 of JLS

Strange Java null behavior in Method Overloading

why the program calls foo(String x) instead of foo(Object x)

That is because String class extends from Object and hence is more specific to Object. So, compiler decides to invoke that method. Remember, Compiler always chooses the most specific method to invoke. See Section 15.12.5 of JLS

If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.

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.

However, if you have two methods with parameter - String, and Integer, then you would get ambiguity error for null, as compiler cannot decide which one is more specific, as they are non-covariant types.

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

Null value in method parameter

In a nutshell, the most specific method among the overloads is chosen.

In this case the most specific method is the one that takes List<Object> since it's a subtype of Object.

The exact algorithm Java uses to pick overloaded methods is quite complex. See Java Language Specification section 15.12.2.5 for details.

Multiple overloaded methods: Does null equal NullPointerException?

If there are several overloaded methods that might be called with a given parameter (null in your case) the compiler chooses the most specific one.

See http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

In your case methodTest(Exception e) is more specific than methodTest(Object e), since Exception is a subclass of Object. And methodTest(NullPointerException e) is even more specific.

If you replace NullPointerException with another subclass of Exception, the compiler will choose that one.

On the other hand, if you make an additional method like testMethod(IllegalArgumentException e) the compiler will throw an error, since it doesn't know which one to choose.

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.



Related Topics



Leave a reply



Submit