Can someone please explain me why the following code listing wont work ??
I understand that in this case boxing cannot be folllowed by widening because if we do that byte will be converted to Byte wrapper object and wrapper classes cannot refer to each other. What im not able to understand is that why cannot widening be followed by boxing i.e. first byte widens to long and then long gets autoboxed to Long wrapper object.
At this moment, widening a byte into long and box it into Long is too much work for the compiler.
I think it may cause out of memory issue. Suppose need some bytes or integers to do a Math. computation, but all the bytes and integers are widened into long. We may not have enough memory if there are a lot of bytes/integers.
For a byte, it has 8 bits. If we widen it into a long, it will be 64 bits. If we have 1000 bytes for example, we will need a lot of memory to hold 1000 long primitives.
Matthew Brown wrote:Because the language designers decided they weren't going to allow it - probably because of the complexity it would introduce (though you'd have to ask one of them to be sure of the real reason).
I think it would create an ambiguous situation when it comes to play in function overloading. For better understanding, Let me modify the code...
If the language designers allowed "primitive widening and wrapper boxing", then compiler would be in an ambiguous situation. Because it is then all possible for a byte to "widening -> boxing" in "short -> Short", "integer -> Integer",
"long -> Long".
subject: Overloading When Combining Widening and Boxing