Possible Duplicate:
Overloaded method selection based on the parameter’s real type
How is an overloaded method choosen when a parameter is the literal null value?
When I execute the code below, I get the following output:
Method with String argument Called ..."
Why?
public class StringObjectPOC {
public static void test(Object o) {
System.out.println("Method with Object argument Called ...");
}
public static void test(String str){
System.out.println("Method with String argument Called ...");
}
public static void main(String[] args) {
StringObjectPOC.test(null);
}
}
Stringclass is a specialized form of theObjectclass (owing to the fact that theStringclass has every feature of theObjectclass in addition to its own) which is chosen by the compiler as specified by the JLS (the most specific method is chosen which is in your case, the one that accepts a parameter of typeString).nullin your case can be resolved to bothStringas well as toObject. Doesn't it? (bothObjectandStringcan benull) Therefore, the most specific method is chosen by the compiler as specified in the Java language specification (JSL) and the most specific method in your case is the one that accepts aStringparameter.