GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dan, a null question from your mock exam. 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 "Dan, a null question from your mock exam." Watch "Dan, a null question from your mock exam." New topic
Author

Dan, a null question from your mock exam.

Kelvin Mak
Ranch Hand

Joined: Oct 04, 2002
Posts: 51
public class Test {
static void m(Object x) {System.out.print("Object");}
static void m(String x) {System.out.print("String");}
public static void main(String[] args) {
m(null);
}
}
The output is : String, cannot understand why pass the parameter null into the m method, it will call the m(String x).
Van i got this thread in your mock exam, could you explain to me?
[ October 15, 2002: Message edited by: Kelvin ]

SCJP1.4
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Kelvin,
Welcome to Javaranch
Please refer to the discussions mentioned below that handle the same topic.
Briefly, when two methods match an invocation expression, the most specific one, that is the one having the most specific parameter list (among other things) is invoked. In this case, The method taking a String argument is invoked because String is more specific than Object.
http://www.coderanch.com/t/239534/java-programmer-SCJP/certification/null-arg-overloaded-method
http://www.coderanch.com/t/239328/java-programmer-SCJP/certification/null-argument-method-being-passed
Finally,
We'd like you to read the Javaranch Naming Policy and change your publicly displayed name (change it here) to comply with our unique rule. Thank you.
PS: Quote from the naming policy:

For your publicly displayed name, use a first name, a space, and a last name. Obviously fictitious names or improperly formatted names may be locked out.


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

Joined: Oct 04, 2002
Posts: 51
thank you, V.C.
 
 
subject: Dan, a null question from your mock exam.