When you don't type cast, it is chosen upon the most specific.
null
can be reference of type String
or an Object
.
So, if both are available then the String method will be called.
public class test {
public static void main(String[] args) {
magic(null);
}
public static void magic(Object o) {
System.out.println("object passed");
}
public static void magic(String s) {
System.out.println("String passed");
}
public static void magic(Integer s) {
System.out.println("Integer passed");
}
}
This would no longer compile,
It would say : Ambiguous method call. Both magic(String)
and magic(Integer)
in test match