Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Conversion of Primitive Data Types

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I was studying "Complete Java 2 Certification Study Guide ", Chapter 4. It clearly states that Primitive data types can be
converted to a "larger data type" e.g from a byte to a short.
This is known as widening conversion. The system will do this for you, its Implicit.
It then later states that a "long" can be converted to a "float".
How is this possible ???
A long is 64 bit BUT a float is 32 bit. This is not a widening conversion, rather its a narrowing conversion and hence will give a compile-time error.
Can somone please shed light on this, maybe a couple of code examples ???

Thanks in Advance.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The value for a long variable will be stored as 64 bits.
However the floating point value will be stored in 32 bits in IEEE floating point format.i.e left most bit for sign bit, next 7 bits from the left for exponent and the remaining 24 bits for mantissa. Thus the value that can be stored using this format(maximum value being 3.4E+038) is much higher than that of the value that can be stored in a simple long variable(max value (2^63)-1). Hence the conversion is widening and not narrowing.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic