This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading When Combining Widening and Boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overloading When Combining Widening and Boxing" Watch "Overloading When Combining Widening and Boxing" New topic
Author

Overloading When Combining Widening and Boxing

gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924
    
    1

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

Joined: Apr 06, 2010
Posts: 4369
    
    8

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

Joined: Nov 01, 2011
Posts: 451
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

Joined: Apr 10, 2012
Posts: 33
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".

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Overloading When Combining Widening and Boxing