Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Dan, a null question from your mock exam.

 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kelvin Mak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, V.C.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic