Precision Level
What is the difference between a float and a double type in Java?
I'm currently covering casting in my programming class (Java), but I'm getting confused with when casting is nece...
Precision Level

What is the difference between a float and a double type in Java?
I'm currently covering casting in my programming class (Java), but I'm getting confused with when casting is necessary when using float or double numbers. I know that float values are 32 bits and double values are 64 (and thus give different levels of precision). What I'm confused is in a case such as this:
float f = (float) 3.14;
Why is casting necessary? Isn't 3.14 already within a float's range; why would it have to be casted again as a float?
Thanks for the help :]
It comes down to the fact that Java has tight type safety.
By default java assumes a manually specified number to be a double, as a double would more precisely cover more numbers that you could type in. You have to cast between doubles and float explicitly. Java is helping you to not do something stupid (loose precision) by making you be absolutely sure about what your doing.
If you want to manually specify floats I always do the following to avoid annoying casting
float f = 3.14f;
|
|
|
Tags: level mage, level precision, precision level
Posted in Calipers & Inspection Guages
You can leave a response, or trackback from your own site.