It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes long -> float conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "long -> float conversion" Watch "long -> float conversion" New topic

long -> float conversion

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hello friends,

see the foll. code snippet.
long l = 5; // 1
float f = l; // 2 This compiles fine!!!
l = f; // 3 This gives compiler error !!!
But as we know that long is 64 bits(8 bytes) & float is 32 bits(4 bytes).
Then how is the statement at line 2 valid without any casting??
and statement at line 3 gives error ? I was expecting the behaviour to be the other way.
Thanx in advance.

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Yi Meng
Ranch Hand

Joined: May 07, 2003
Posts: 270
Originally posted by Vishy Karl:

But as we know that long is 64 bits(8 bytes) & float is 32 bits(4 bytes).

The meaning for these bits are different. for long, you know that the max is 2^63 - 1 whereas for float, its 32 bits are conform to the IEEE 754 standard. Just google "IEEE 754" and you will find lots of info about the standard and particularly the bit representation. in short, the range that a float can represent is far wider than long ....that's why a long can be implicitly cast to float and not vice versa.
you may just use the link i find for you here.

Meng Yi
I agree. Here's the link:
subject: long -> float conversion
It's not a secret anymore!