Monday, 16 May 2016

Difference between float and double variable in Java?

Though both float and double datatype are used to represent floating point numbers in Java, a double data type is more precise than float. A double variable can provide precision up to 15 to 16 decimal points as compared to float precision of 6 to 7 decimal digits. Another significant difference between float and double is their storage requirement, double is more expensive than float. It takes 8 bytes to store a variable while float just takes 4 bytes. Which means, if memory is constraint than its better to use float than double. BTW, the double type also has larger range than float and if your numbers don't fit well in float then you have to use double in Java. It's also worth noting that floating point numbers or real numbers are by default double in Java. If you want to store them into float variable, you need to either cast them or use a prefix 'f' or 'F' as shown in our example.
Read more »

No comments:

Post a Comment