This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing null as a method parameter

 
Thor Silva
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leandro Oliveira
Ranch Hand
Posts: 298
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey folks, thanks very much ... I think I need to read the Java Language Specification book before taking the test !
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic