Javatpoint Logo
Javatpoint Logo

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:

  • float Data Type
  • double Data Type

Both, float and double represents the floating-point numbers that store the decimal values.

Floating-Point Data Type Values Size (bits)* Storage Requirement (bytes) Default Value Precision Decimal Digits Range Accuracy
float IEEE 754 Floating-Point 32 4 0.0f Single 6 decimal digits 3.4e-038 to 3.4e+038 Low
double IEEE 754 Floating-Point 64 8 0.0d Double 15 decimal digits 1.7e-308 to 1.7e+308 High

*The size bits include the following:

Bits float double
Sign 1 1
Exponent 8 11
Mantissa 23 52

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 Vs Double Java

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.

Basis float Data Type double Data Type
Memory It occupies 4 bytes. It occupies 8 bytes.
Accuracy Its accuracy is low. Its accuracy is high.
Precision It follows single-precision (6-7 decimal digits). It follows double-precision (15-16 decimal digits).
Keyword used The float keyword is used to define a float number. The double keyword is used to define a double-precision number.
Wrapper Class Its wrapper class is java.lang.Float. Its wrapper class is java.lang.Double.
Default Data Type Java does not use it as the default floating-point number. It is the default data type for floating-point numbers.
Data Loss There will be no data loss if we convert float to double. There will be data loss if we convert double to float.
Uses It should be used where less accuracy is required and storage is a constraint. It is used where more accuracy is required and also requires more precision.
Suffix It uses F or f as a suffix. It is mandatory to add a suffix if you are declaring a float variable. It uses d or D as a suffix. It is optional to add a suffix if you are declaring a double variable.
Representation 28.96f or 28.96F 12.5 or 12.5D or 12.5d

Similarities Between float and double Data Type

  • Real numbers can be represented by both data types.
  • Both float and double data types are not precise, hence, they are approximate value.

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.

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 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.


Next TopicStack Vs Heap Java





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA