This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Box and Widen

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Both the above methods are eligible for Box and Wide but still when run Number Called is the Output. - I dont understand why..
1 more question - Number IS-A object is both the methods are considered to be overloaded?

Thanks in advance..
-Jerry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The intcan only be boxed to an Integer, there is no widening going on. The class hierarchy is Object -> Number -> Integer. The rules for choosing from overloaded methods dictate that the method with the most specific parameter type is chosen. In this case Integer is more specific than Object so method (Integer i) is chosen.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,


The hierarchy is int(primitive) --> Integer -- > Number --> Object.

When you pass any argument in the method , first it will call the lowest level. (lowest subclass) . So it is calling Number . Because number is the lowest subclass(es) in the existing ones.

If you want to clarify it , then remove the method which is taking number, then "Object called" will be displayed.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rami reddy marri:
Hi ,


The hierarchy is int(primitive) --> Integer -- > Number --> Object.

When you pass any argument in the method , first it will call the lowest level. (lowest subclass) . So it is calling Number . Because number is the lowest subclass(es) in the existing ones.

If you want to clarify it , then remove the method which is taking number, then "Object called" will be displayed.




The primitive int is not in any class hierarchy. Only classes can be in a hierarchy.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok berry.

I just mentioned that while passing parameters , sending int (primitive value) . so i mentioned that it will convert from primitive int to Integer class in 1.5 .
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! 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
reply
    Bookmark Topic Watch Topic
  • New Topic