This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft 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

Win a copy of Java 8 in Action this week in the Java 8 forum!
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
Similar Threads
Boxing & Widening
K&B Mock Question 5
varargs doubt
Question on widening ,boxing
Widening beats boxing?