Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusion about autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusion about autoboxing" Watch "Confusion about autoboxing" New topic
Author

Confusion about autoboxing

Abdullah Mamun
Ranch Hand

Joined: Mar 19, 2007
Posts: 99



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 ]

MooN
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
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.
Louis Moloney
Ranch Hand

Joined: Feb 06, 2007
Posts: 59
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 ]

Veeraraagavan Rajachandrasekhar
Greenhorn

Joined: Jun 19, 2007
Posts: 8
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 ]
 
Consider Paul's rocket mass heater.
 
subject: Confusion about autoboxing