Why does it (apparently) make a difference whether I pass null
as an argument directly, or pass an Object
that I assigned the value null
?
Object testVal = null;
test.foo(testVal); // dispatched to foo(Object)
// test.foo(null); // compilation problem -> "The method foo(String) is ambiguous"
public void foo(String arg) { // More-specific
System.out.println("foo(String)");
}
public void foo(Object arg) { // Generic
System.out.println("foo(Object)");
}
In other words, why is the (commented-out) second call to foo(...)
not dispatched to foo(Object)
?
Update: I use Java 1.6. I could compile Hemal's code without problems, but mine still doesn't compile. The only difference I see is that Hemal's methods are static while mine are not. But I really don't see why this should make a difference...?
Update 2: Solved. I had another method foo(Runnable) in my class, so the dispatcher couldn't unambiguously select the single most specific method. (See my comment in Hemal's second answer.) Thanks everyone for your help.
Because the second commented out invocation with null is ambiguous to the compiler. The literal null could be a string or an object. Whereas the assigned value has a definite type. You need to cast the null, e.g. test.foo((String)null) to remove the ambiguity.
Which version of Java are you using? With 1.6.0_11 the code (pasted below) compiles and runs.
I am sure its obvious why
foo(testVal)
goes tofoo(Object)
.The reason why
foo(null)
goes tofoo(String)
is a little complex. The constantnull
is of typenulltype
, which is a subtype of all types. So, thisnulltype
extendsString
, which extendsObject
.When you call
foo(null)
compiler looks for the overloaded method with most specific type. SinceString
is more specific thenObject
that is the method that gets called.If you had another overload that was as specific as String, say
foo(Integer)
then you would get a ambiguous overload error.Sorry to use an answer, for a comment, but I need to post code that won't fit in comment.
@Yang, I am also able to compile and run the following. Can you post a complete code that compiles with one line commented such that if I uncomment that line it won't compile?
Has anyone tried the example ???
With 1.6.0 foo(null) is dispatched to the most specific method applicable which is foo(String)...
If you add a new method say foo(Integer) the compiler cannot choose the most specific applicable method and shows an error.
-Patrick