It is possible for the compiler to perform a boxing operation followed by an extension operation to match the method call. Take an example
class BoxAndWiden { static void go(Object o) { Byte b2 = (Byte) o;
Compiles (!) And produces the result: 5 Let me show you how this works behind the scenes. When the JVM hits the line that calls the go () method:
- Byte b is placed in byte.
- The byte reference has been expanded to an object (since Byte extends Object).
- The go () method received an Object reference, which actually refers to the byte object.
- The go () method returns the Object link back to the Byte link (re member, in this script there has never been an object of type Object, only an object of type Byte!).
- The go () method printed the byte value.
But in your case. Why didn't the compiler try to use the box-then-expand logic when it tried to cope with the WidenAndBox class?
if he first tried to check the box, the byte would be converted to byte. Now we are again trying to expand the byte to a long one, and, of course, the IS-A test fails.
source share