This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Widening and boxing
It's not a secret anymore!