• 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

Question about overloading (OCPJP) Boyarsky & Selikoff

 
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have a question on page 4. It says that when overloading methods, Java will first look at a matching superclass type, and then later to an autoboxed type. However, in my example, it's the other way around. It prints the autoboxed type instead of the superclass type.

 
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does int type inherit from Number? Nope. 'int' is a primitive data type, which is different from usual Class type. Look which classes actually inherit from Number. If method with Integer signature wouldn't be there, then behind the scenes I believe int would be wrapped to Integer and only then method signature with Number would be invoked.
 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Integer does inherit from Number:

According to the documentation "The direct subclasses of Number are AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, DoubleAccumulator, DoubleAdder, Float, Integer, Long, LongAccumulator, LongAdder, Short"
 
Helene Shaikh
Greenhorn
Posts: 21
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay thanks for the answer!
 
Liutauras Vilda
Marshal
Posts: 8856
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right. So, but not an int. int and Integer aren't the same. When you pass 3, you pass an 'int'. So, it gets autoboxed and method with Integer signature is being used. If you were to remove method with signature Integer, then still 3 would get autoboxed to an Integer, and then would happen what you read in the book, it would use its superclass signature Number, which Integer extends.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this topic you'll find 15 small code snippets to test your knowledge about method overloading
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic