File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sam Wang
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
}
}
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sam Wang
Ranch Hand
Posts: 95
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know.Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic