Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overloading question

 
Paula Jiang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will happen when you compile and run the following code?
 
Mellihoney Michael
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler will generate an error because the call

in the main() method is ambiguous.It means the complier don't know what method to call when it bounds the parameter "null"!
the

or the

?
I just compiled it
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
compiler will generate an error because the call

Compiler will not generate any error. Infact the program also runs and gives the result of m2.
This is because the compiler will map null to the "most specific object" that null can be cast into which is String.
The rule is String is-a Object but Object is-not-a String .
so the null is cast into a string.
I remember reading it somewhere in JLS via one of Dan's mock exams. Search the post and you will find the solution in one of the posts
Thanks
Sri
[ February 15, 2003: Message edited by: Sri Sri ]
 
Paula Jiang
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles and runs fine just like Sri Sri said, but I want to know why. Thanks Sri sri
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Related discussions:
null arg in overloaded method
null argument to a method - what is being passed??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic