File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Widening and boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Widening and boxing" Watch "Widening and boxing" New topic

Widening and boxing

J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
I ran the following code,

and I got the same answer as the book, 4 3. However, I don't understand why
the answer is not 3 3. Since s is of type Short, couldn't you box short to Short, then widen Short to Integer? The only answer I can think of is maybe because Short is now an object not a primitive, it is a subclass of Number, not Integer? Is that correct?
Stephen O'Kane

Joined: Aug 17, 2005
Posts: 26
you can widen from short to int, but not from Short to Integer, so your reckoning is correct
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
I agree. Here's the link:
subject: Widening and boxing
It's not a secret anymore!