• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

primitive conversion/assignment conversion?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
RHE states that for primitive assignment conversion, a primitive(non-boolean) may be converted to another primitive(non-boolean) provided the conversion is a "widening conversion." (p. 103.) RHE goes on to say that Java's widening conversions are from a byte to a short, an int, a long, a float, or a double.
This makes sense because you avoid accidently squeezing a larger number into a smaller(narrower) data type with a resulting loss of information. (You can always use cast to expressly do this)
Obviously, a float (32 bit) is not wider than a long (64 bit). However, I tried this in code and a long can be converted to a float by assignment. Conversely, RHE says you cannot by assignment convert a float to a long because it would be a "narrowing conversion." Am I missing something? I would appreciate if somebody could tell me why this apparent exception to the rule exists?
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jim did a good job of answering this here http://www.javaranch.com/ubb/Forum24/HTML/000617.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic