File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes float to long typecasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "float to long typecasting" Watch "float to long typecasting" New topic

float to long typecasting

Sandeep Kumar.

Joined: Jul 23, 2008
Posts: 9

i don't know how java stores floating point numbers in memory.(distribution of absolute and fraction digits)
a long takes 8 byte while a float takes 4 bytes only so why can't we typecast a float to a long implicitly. why widening isn't followed here in this case

and why a long can be type casted to a float implicitly while long takes 4 bytes more than float so there must be a loss of precision in this case; still it is allowed.

Henry Wong

Joined: Sep 28, 2004
Posts: 20535

Whether casting can be done implicitedly depends on the range -- not on the size in bytes or the precision. And since the range of the float is larger than the range of the long, a long can be casted to a float implicitedly, even though you may lose precision by doing so.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: float to long typecasting
It's not a secret anymore!