Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about null

 
wei liu
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
The code does not compile.
The code compiles cleanly and shows "Object Version".
The code compiles cleanly and shows "String Version"
The code throws an Exception at Runtime.
the ans is 3rd.
i thought if the code were written like 'questin.method("null")', the ans were 3rd, but .... i dont know why?
 
Vikrama Sanjeeva
Ranch Hand
Posts: 756
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In method overloading compiler looks for the most specific method in selection.Here null can be easily passed to String version.
However it can pass to Object versions too but its not more specific than Strings version.
Bye.
Viki.
------------------
Count the flowers of ur garden,NOT the leafs which falls away!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic