• 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 test

 
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can any one please let me know why compile time error is not generated when a long is assigned to a float ? Float is of size 32 bits but long is of size 64 bits.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,

This is to do with primitive widening conversion (google-whack it).

You only lose precision (i.e. lose significant bits) and in Java, that's acceptable.


Hope this helps.

AO
 
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
float is 32 bits and long is 64 bits but
storage mechanism is not same

try
System.out.print(Float.MAX_VALUE);
System.out.print(Long.MAX_VALUE);

or

float f=Long.MAX_VALUE;
System.out.println(f); // exponent is 18
System.out.println(Float.MAX_VALUE); // exponent is 38

float uses IEEE754 storage mechanism hence can store number larger in magnitude than long.

word magnitude is more important.
hope this helps and correct me if I am wrong
bye.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic