• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overloading When Combining Widening and Boxing

 
gurpeet singh
Ranch Hand
Posts: 924
1
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can someone please explain me why the following code listing wont work ??

Code Listing:



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.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Helen Ma
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

The computers may not have enough memory.
 
Md. Minhajur Rahman
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic