Wednesday, July 26, 2023

Difference between float and double variable in Java? Example

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. This means if memory is a constraint then it's better to use float than double. By the way, the double type also has a 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 the float variable, you need to either cast them or use the prefix 'f' or 'F' as shown in our example.


float and double in Java - Similarities

Here are some common attributes of both float and double data types in the Java programming language:

1.Real Numbers
Both double and float are used to represent real numbers in Java i.e. numbers with fractions or decimal points.

2. Approximate types
Both double and float are approximate types, they are not precise.

3. Comparision
You should use logical operators like  > or < to compare both float and double variables, instead of = and != because they are not precise.

See this article for more details about comparing float and double variables in Java.




Difference between float vs doubles in Java

Here are some key differences between float and double in Java :

1. The double data type is more precise than float in Java.

2. double-takes more space than float in Java. double needs 64-bit storage compare to 32-bit storage of float data type.

3. double has a higher range than float, sure because it got more bits to store data.

4. float uses 1 bit for sign, 8 bits for the exponent, and 23 bits for mantissa, while double uses 1 bit for a sign, 11 bits for the exponent, and 52 bits for mantissa.

5. By default, floating-point numbers are double in Java. In order to store them into a float variable, you need to cast them explicitly or suffix with 'f' or 'F' as shown below :

public static final float PIE = 3.14; // compile time error

Use cast
public static final float PIE = (float) 3.14;

or suffix 'f' or 'F'

public static final float PIE = 3.14f;
public static final float GRAVITY = 9.8F;



When to use double and float in Java?

Though both can be used to represent floating-point numbers, there are a couple of things you can consider to choose between double and float. Though both are approximate types, If you need more precise and accurate results then use double.

Use float if you have memory constraints because it takes almost half as much space as double. If your numbers cannot fit in the range offered by float then use double. Though be careful with floating-point calculation and representation, don't use double or float for monetary calculation, instead use BigDecimal.


Difference between float and double variable in Java?



That's all about the difference between float and double in Java. Remember, by default floating-point numbers are double in Java, if you want to store them into float variables, you need to either cast them explicitly or suffixed them using the 'f' or 'F' characters.

It's also best practice to choose a data type that takes less storage if it's sufficient for data you are storing, so choose float over double if you are happy with precision and range, double is more accurate than float though.


5 comments :

Anonymous said...

Two of the main difference between float and double is that

(i) Float requires 4 bytes of storage while double requires 8 bytes.
(ii) Float is of single precision while double is of double precision

Unknown said...

very useful.....thanks

Unknown said...

So, if the range of values for DOUBLE is from -1.7D308 to +1.7D308, what are the minimum values for the exponents? -308 to +308???

Rajat said...

public static final float PIE = (float) 3.14;

I think , compiler may show the error, just beacause of narrow conversion is not allowed in java.

ICC FTP SERVER said...

Very useful, thanks!!

Post a Comment