Float Vs Double JavaIn 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 nonprimitive. Primitive data type includes all the predefined data types such as Integer, Character, Boolean, FloatingPoint, etc. while the nonprimitive datatype includes userdefined 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 FloatingPoint data types:
Both, float and double represents the floatingpoint numbers that store the decimal values.
*The size bits include the following:
SinglePrecision: It consists of one sign bit (S), eight exponent bits (E), and twentythree mantissa bits (M). DoublePrecision: It consists of one sign bit (S), eleven exponent bits (E), and fiftytwo mantissa bits (M). float Data TypeIt is a 32bit, singleprecision IEEE 754 (Standard for FloatingPoint Arithmetic) floatingpoint number. It means that it gives 67 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 TypeThe double data type is a 64bit doubleprecision IEEE 754 floatingpoint number. It means that it gives 1516 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 TypeThe 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 floatingpoint 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 halfspace 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. FloatDataTypeExample.java Output: x/y = 0.33333334 DoubleDataTypeExample.java Output: x/y = 0.3333333333333333 Through the above two examples, it is clear that the double data type takes more memory to store a doubleprecision number and also gives the more accurate result up to 16 decimal digits. While the float data type takes less space to store singleprecision numbers and it gives results up to 6 decimal places.
Next TopicStack Vs Heap Java
