• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overloading When Combining Widening and Boxing

 
Ranch Hand
Posts: 924
1
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".

 
See where your hand is? Not there. It's next to this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic