Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I came across this question in a quiz,

public class MoneyCalc {     public void method(Object o) {       System.out.println("Object Verion");    }     public void method(String s) {       System.out.println("String Version");    }     public static void main(String args[]) {       MoneyCalc question = new MoneyCalc();       question.method(null);    } } 

The output of this program is "String Version". But I was not able to understand why passing a null to an overloaded method chose the string version. Is null a String variable pointing to nothing ?

However when the code is changed to,

public class MoneyCalc {     public void method(StringBuffer sb) {       System.out.println("StringBuffer Verion");    }     public void method(String s) {       System.out.println("String Version");    }     public static void main(String args[]) {       MoneyCalc question = new MoneyCalc();       question.method(null);    } } 

it gives a compile error saying "The method method(StringBuffer) is ambiguous for the type MoneyCalc"

like image 495
zakSyed Avatar asked Oct 23 '12 14:10

zakSyed


People also ask

What are the three rules on which a method can be overloaded?

With respect to the method it overrides, the overriding method must follow following mandatory rules: It must have the same method name. It must have the same arguments. It must have the same return type.

Do overloaded methods need parameters?

Method overloading can be done by changing: The number of parameters in two methods. The data types of the parameters of methods. The Order of the parameters of methods.

How can methods be overloaded?

Method overloading can be achieved by the following: By changing the number of parameters in a method. By changing the order of parameters in a method. By using different data types for parameters.

When it is decided that method should be overloaded?

If a class has multiple methods having same name but different in parameters, it is known as Method Overloading. If we have to perform only one operation, having same name of the methods increases the readability of the program.


1 Answers

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.

like image 188
Jon Skeet Avatar answered Sep 23 '22 09:09

Jon Skeet