Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Primitive test

 
Srinivasa Raghavan
Ranch Hand
Posts: 1228
  • Mark post as helpful
  • send pies
  • 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.
 
Annette Sovereign
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Santana Iyer
Ranch Hand
Posts: 335
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic