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.
Additionally, the JLS 3.10.7 also declares that "null" is a literal value of the "null type". Therefore there exists a type called "null".
Later, the JLS 4.1 states that there exists a null type of which is impossible to declare variables, but you can use it through the null literal only. Later it says:
The null reference can always undergo a widening reference conversion to any reference type.
Why the compiler chooses to widen it to String might well be explained in Jon's answer.
You can assign a string
to a null
value so it is valid and the order for java and most programming languages is fit to the closest type and then to object.
To answer the question in the title: null
is neither a String
nor an Object
, but a reference to either can be assigned to null
.
I'm actually surprised this code even compiles. I tried something similar previously and I got a compiler error saying that the call was ambiguous.
However, in this case, it seems like the compiler is choosing the method which is lowest on the food chain. It's assuming that you want the least generic version of the method in order to help you out.
I'll have to see if I can dig up the example where I got a compiler error in this (seemingly) exact same scenario, though...]
EDIT: I see. In the version I made, I had two overloaded methods accepting a String
and an Integer
. In this scenario, there is no "most specific" parameter (as in Object
and String
), so it can't choose between them, unlike in your code.
Very cool question!