File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

float to long typecasting

Sandeep Kumar.
Greenhorn

Joined: Jul 23, 2008
Posts: 9
Hi

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.

SCJP 6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18843
    
  40

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.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: float to long typecasting