Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes passing null as a method parameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "passing null as a method parameter" Watch "passing null as a method parameter" New topic
Author

passing null as a method parameter

Thor Silva
Greenhorn

Joined: Mar 06, 2003
Posts: 14
Hello All, I'm new here . I'm studing to be an jcp and yesterday I found this great site . I hope somebody can helps me and if I can , helps somebody . I was taking an mock exam and got the following question
public class Test
{
public void m1(Object o)
{
System.out.println("Inside the object method");
}
public void m1(String o)
{
System.out.println("Inside the string method");
}

public static void main(String[] args)
{
Test t = new Test();
t.m1(null);
}
}
The result was "Inside the string method" , but if I comment this method , the result turns to be "Inside the object method" . Why this happens ?


Have a nice day and TIA<p> From<p> Thor
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Please check out the following discussions which handle the same topic:
null arg in overloaded method
null argument to a method - what is being passed??
Hope this helps.


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

Joined: Nov 07, 2002
Posts: 298
wellcome to the ranch, thor
java language especification, chapter 15, i guess, states that in these situations, where an argument is ambiguos, the most specific method will be chosen. In this case we have:
Object
|
--->String
String is a subclass of Object. String is an object. String isn't only an object, a string is an Object of class String. (i'm trying to say that string is more specific than just a simple Object)
So the string method will be chosen!!!
hope it helps. Attention, if there is no more specific method, a compile time error will ocurr.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
java language especification, chapter 15, i guess, states that in these situations, where an argument is ambiguos, the most specific method will be chosen.
That's correct and here is the link to the JLS section explaining that point:
JLS 15.12.2.2 Choose the Most Specific Method.
I have tried once to provide a "plain-english" explanation of the method resolution process in the Javaranch newsletter. Here is the link to the article: JR Newsletter: JLS 15.12 (Method Invocation Expressions) in Plain English
[ March 06, 2003: Message edited by: Valentin Crettaz ]
Thor Silva
Greenhorn

Joined: Mar 06, 2003
Posts: 14
Hey folks, thanks very much ... I think I need to read the Java Language Specification book before taking the test !
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
No, you do not need to read it before taking the exam. However it is useful as a reference to try the hard points (*). Later, when you are a SCJP, yes I recommend its reading.
(*) The hard points are soften here in the Ranch.
[ March 06, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
 
subject: passing null as a method parameter
 
Similar Threads
what is the out put of this program ? why ?
static method overriding in same class
why is String method called not Object method....?
Meaning of Null passed for an object reference
Passing null references