• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Overloading with Boxing Confusion

 
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everyone,



Why the compiler chose method with argument Number. Even Object was a perfect candidate to be selected. Can someone explain me the reason?
Thanks,
Samar
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samar Bir wrote:
Why the compiler chose method with argument Number. Even Object was a perfect candidate to be selected. Can someone explain me the reason?



The section in the JLS is a bit complicated, but if you want to read it ...

http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5

The compiler choose the overloaded method, by a criteria called the "most specific method". It also does a lot of other things, but those are not relevant to your question.

Henry
 
Samar Bir
Greenhorn
Posts: 23
IntelliJ IDE Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Henry,

Thank you very much. I understood the concept. Thanks a ton for your help.

BR,
Samar
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I updated the code like this:



Now the output is "Integer called"
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic