• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Confusion about autoboxing

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


I was expecting, line 7 will print "Method 2" but the actual output is "Method 1".
I think its because of "byte s" is wrapping to Byte object and since there is no such method with this type it is calling the method with Number type parameter.
I want to know if there is any other explanation.
[ July 06, 2007: Message edited by: Abdullah Al Mamun ]
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Abdullah Al Mamun:
I think its because of "byte s" is wrapping to Byte object and since there is no such method with this type it is calling the method with Number type parameter.

Your assumption is correct.
 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte will never box to Integer as the Number subclasses are peers
ie Integer does not subclass Byte
eg

[ July 06, 2007: Message edited by: Louis Moloney ]
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your assumption is right.....If you want a more detailed answer read the next few lines.

byte is autoboxed to Byte.
In the code there is no method with a Byte,Byte argument.
A "Byte" cannot be autoboxed to an "Integer" since they(these Wrapper Classes) are different from each other. Remember that the following classes extend "Number" class:
AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short. Therefore, a "Byte" is a "Number" and hence the output "Method1".
[ July 06, 2007: Message edited by: Veeraraagavan Rajachandrasekhar ]
 
Ever since I found this suit I've felt strange new needs. And a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic