aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why? null is not a common object,then what it is? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why? null is not a common object,then what it is?" Watch "Why? null is not a common object,then what it is?" New topic
Author

Why? null is not a common object,then what it is?

Sam Wang
Ranch Hand

Joined: Jul 17, 2001
Posts: 95
public class Test {
void doo(Object obj){
System.out.println("object");
}
void doo(String s){
System.out.println("string");
}
public static void main(String[] args) {
new Test().doo(null);
}
}


SCJP(2001)
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please have a look at the following discussion which handles the same topic:
http://www.coderanch.com/t/239328/java-programmer-SCJP/certification/null-argument-method-being-passed


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
null is a literal which is used to give a value to a reference that does not refer to anything.

The compiler has to choose which of those doo() methods to call. Because String is a subclass of Object, String is said to be "more-specific" than object. The compiler will call the doo()method with the most specific formal parameter type.
In this case doo(String) rather than doo(Object) will be chosen.
-Barry


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Sam Wang
Ranch Hand

Joined: Jul 17, 2001
Posts: 95
I know.Thanks.
 
 
subject: Why? null is not a common object,then what it is?