Overloading method calls with parameter null
java, overloading
Solution
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.
Problem
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); } } ```