• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Implicit conversions...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is this legal when a float is 32 bits and a long is 64 bits?
long number = 23;
float f1 = number;
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check out this thread about the very same question:
why can a long assigned to float without casting
enjoy!
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The rang of long is
For long, from -9223372036854775808 to 9223372036854775807, inclusive
Where as float values can be represented with float-extened-exponent,
which in tern effectively store larger numbers than long primitive range.
java implementation automatically decides where to use float-extened-exponent or not.
In your example, it does need to use float-extened-exponenet.
for example
float f =Long.MAX_VALUE;
System.out.println(f);//will print 9.223372E18
long l=24;
f=1;
System.out.println(f);//will print 24.0

There is a enough room in float-exponential format to store all the rang of long integers.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a enough room in float-exponential format to store all the rang of long integers.
Enough to hold the range -- but not the detail. In this code snippet two very different longs get converted into the same float:
 
reply
    Bookmark Topic Watch Topic
  • New Topic