Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt from K&B book..Please elaborate.... Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Doubt from K&B book..Please elaborate...." Watch "Doubt from K&B book..Please elaborate...." New topic
Author

Doubt from K&B book..Please elaborate....

Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Hi ,

Our problem was of coverting a Byte to Integer


After this it has been told we are not allowed to do wideing and then boxing but reverse is true.. and the boxing followed by widening example given is as below , i am okay with example :




My doubt is that is our original problem of converting from byte to Long is resolved (as was in first example ) ? as it is not rectified in second example ( in the second example we just achieved conversion from byte to Byte..)

Please put some light on it...

Thanks
Vishal







[BPSouther: Added code tags]
[ February 14, 2008: Message edited by: Ben Souther ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As you know, if boxing or unboxing is involved in method conversion, the boxing or unboxing must occur before any upcast or widening. (See JLS - 5.3 Method Invocation Conversion.)

Your second example works because the primitive byte b can be boxed to a Byte, and then the Byte reference can be upcast to type Object (because Byte is a subclass of Object).

But your first example is different because Byte is not a subtype of Long. Both of these are direct subclasses of Number. Therefore, a Byte reference cannot be upcast to type Long.
[ February 15, 2008: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Vishal Chugh
Ranch Hand

Joined: Oct 27, 2007
Posts: 177
Hi Marc,

Thanks a lot!!!
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Doubt from K&B book..Please elaborate....
 
Similar Threads
Regarding Widening and Boxing
Doubt in Sierra Bates chapter 3
boxing and widening doubt cant understand the logic for this program
getting confused
Widening and Autooboxing