Float Vs Double Java
In Java, data types specify the size and type of values. It is used to store the floating values of an identifier. Data types are classified into two categories, primitive and non-primitive. Primitive data type includes all the predefined data types such as Integer, Character, Boolean, Floating-Point, etc. while the non-primitive data-type includes user-defined data types such as Classes, Arrays, and Interfaces, etc. Both float and double data types store floating values but still, they are different. In this section, we will learn the differences between float and double datatype in Java.
There are two types of Floating-Point data types:
Both, float and double represents the floating-point numbers that store the decimal values.
*The size bits include the following:
Single-Precision: It consists of one sign bit (S), eight exponent bits (E), and twenty-three mantissa bits (M).
Double-Precision: It consists of one sign bit (S), eleven exponent bits (E), and fifty-two mantissa bits (M).
float Data Type
It is a 32-bit, single-precision IEEE 754 (Standard for Floating-Point Arithmetic) floating-point number. It means that it gives 6-7 decimal digits precision. It is used if we want to use memory effectively because it takes less memory in comparison to double data type. To define a float value, we must use a suffix f or F. Its default value is 0.0f. By default, float numbers are treated as double in Java.
For example, if we define a float number as:
The above declaration of float variable gives the compilation error. We can correct the error by adding a suffix f or F.
double Data Type
The double data type is a 64-bit double-precision IEEE 754 floating-point number. It means that it gives 15-16 decimal digits precision. It consumes more memory in comparison to the float data type. It is used to store decimal values. Its default value is 0.0d. It is optional to add suffix d or D. For example:
float Vs double Data Type
The double data type is more accurate than the float data type. The following table summarizes the differences between float and double data types.
Similarities Between float and double Data Type
Which floating-point data type in Java we should use?
double is more precise than float. So, if a more precise and accurate result is required use double. Another reason to use double is that if the number is not fitting in the range offered by the float then use double. We should use float if we have memory constraint because it occupies half-space than double.
We recommend you to use double over float if there is no memory and space constraint and when more precision is needed. It is advisable to go with the float, if memory is a concern, and the result in 16 precision decimal digits are not required.
The following two Java programs clearly show the differences between float and double data type.
x/y = 0.33333334
x/y = 0.3333333333333333
Through the above two examples, it is clear that the double data type takes more memory to store a double-precision number and also gives the more accurate result up to 16 decimal digits. While the float data type takes less space to store single-precision numbers and it gives results up to 6 decimal places.