Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

overloading question

 
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • 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??
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic