• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

overloading question  RSS feed

 
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?
 
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
 
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
 
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??
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!