Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading method calls with parameter null [duplicate]

Possible Duplicate:
Method Overloading for NULL parameter

In the code below the output is

String

and if I remove the method with the parameter of type String then the output is

Object

I know how overloading of methods acts when the parameter types don't match exactly but I can not understand how null can be treated as an Object and/or a String parameter.

What is the explanation for this?

class C {

    static void m1(Object x) {
        System.out.print("Object");
    }
    static void m1(String x) {
        System.out.print("String");
    }

    public static void main(String[] args) {
        m1(null);
    }
}
like image 229
Sudeepta Avatar asked Jul 24 '12 16:07

Sudeepta


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.

Can we pass null as a parameter?

You can pass NULL as a function parameter only if the specific parameter is a pointer. The only practical way is with a pointer for a parameter. However, you can also use a void type for parameters, and then check for null, if not check and cast into ordinary or required type.

Can you overload two methods with the same number of parameters?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

What methods are called overloaded?

In Java, two or more methods may have the same name if they differ in parameters (different number of parameters, different types of parameters, or both). These methods are called overloaded methods and this feature is called method overloading.


2 Answers

It always uses the most specific method according to the Java specs, section 15.12.2.5.

The intro is reasonably specific about it:

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.

Generally speaking, and at least for code readability, it's always best to try to be as explicit as possible. You could cast your null into the type that matches the signature you want to call. But that's definitely a questionable practice. It assumes everyone knows this rule and makes the code more difficult to read.

But it's a good academic question, so I +1 your question.

like image 138
mprivat Avatar answered Oct 11 '22 11:10

mprivat


When multiple overloads match a signature, Java picks the most specific method from among them.

The value of null matches both Object and String, but String is a subclass of Object, so String is picked. If you add another overload with a sibling of String in the class hierarchy, you'd get a compile error.\

// DOES NOT COMPILE
class C {
    static void m1(Object x) {
        System.out.print("Object");
    }
    static void m1(String x) {
        System.out.print("String");
    }
    static void m1(Integer x) {
        System.out.print("Integer");
    }
    public static void main(String[] args) {
        m1(null);
    }
}

Here is a link to a post that discusses your code example at some length.

like image 25
Sergey Kalinichenko Avatar answered Oct 11 '22 09:10

Sergey Kalinichenko