File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets 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: 4342
    
    7

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".

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading When Combining Widening and Boxing
 
Similar Threads
Doubt in Sierra Bates chapter 3
Widening and Boxing....
boxing and widening
Why varags method called first
Widening and Autooboxing