7 Difference Between float and double Datatypes in Java

double and float are datatypes in java. Although both represent floating-point literals still there exist some minor differences between them. In this post, I will be sharing the difference between float vs double in java, when to prefer float over double or double over the float, similarities between them and java code examples.

Read Also : Best way to Calculate Maximum and Minimum value of Datatypes

What are literals?
Any constant value which is assigned to a variable is called a literal. In other words, literals are constants.
Java has five types of literals i.e., integer literals, boolean literals, Character literals, String literals, and floating-point literals.
float and double are called floating-point literals.

Difference between float and double in Java

1. Memory : float takes 4 bytes (32 bits) where as double takes 8 bytes(64 bits).
Below is the java program to print size a float and a double takes.
For float:

public class FloatSize {
    public static void main(String args[]) {
      System.out.print("float size is : ");
      System.out.print(Float.SIZE + " bits " + " or ");
      System.out.println(Float.SIZE/8 + " BYTES"); // 1 BYTE = 8 bits
    }
}

Output :
float size is : 32 bits or 4 BYTES
 
For double:

public class DoubleSize {
    public static void main(String args[]) {
      System.out.print("double size is : ");
      System.out.print(Double.SIZE + " bits " + " or ");
      System.out.println(Double.SIZE/8 + " BYTES"); // 1 BYTE = 8 bits
    }
}

Output:
double size is : 64 bits or 8 BYTES
 

2. Suffix : By default, float numbers are double in java. In order to store them into float variable, you need to explicitly add the suffix 'f' or 'F'.

Suppose we have a number 1.23456. Let's see what happens if we store it in the float variable num.

float num = 1.23456 // compiler error

There are two options to correct the above compiler error.

First by adding 'f' or 'F' at the end of the number

float num = 1.23456f
// or
float num1 = 1.23456F

Second by typecasting it to float, i.e

float num = (float) 1.23456

For double you need to explicitly add the suffix 'd' or 'D'.

0.5 , 0.5d or 0.5D


3. Precision : float is a single precision floating point operation. In other words, a float can give you 6-7 digits decimal points precision.
double is a double precision floating point operation. In other words, double can give you 15-16 decimal points precision.
You can find the details about the single and double precision floating point operation here.

public class FloatPrecision {
    public static void main(String args[]) {
      float num = 1.2345678f;    
      System.out.println("float precision upto 6-7 digits after decimal : ");
      System.out.print(num);
    }
}

Output:
float precision upto 6-7 digits after decimal : 
1.2345678

public class DoublePrecision {
    public static void main(String args[]) {
      double num = 1.2345678910111213;    
      System.out.println("double precision upto 15-16 digits after decimal : ");
      System.out.print(num);
    }
}

Output:
double precision upto 15-16 digits after decimal : 
1.2345678910111213

4. Data loss : There will be no data loss when the float is converted to double as float has a lower range than double.
Data loss is expected when double is converted to float.

5. Default datatype : float is not the default data type value of floating point literals.
double is the default data type of floating point literals.

6. Wrapper class : Wrapper class for float is java.lang.Float.
Wrapper class for double is java.lang.Double.

7. Defaut value : float default value is 0.0f.
double default value is 0.0d.

Similarities between float and double

1. Real numbers in java are represented by both float and double i.e numbers with fractions or decimal points.

2. float and double both have approximate values as they are not precise.

When to prefer float and double in java

double is more precise than float and provides with up to 16 decimal points of precision when
compared to float which provides a maximum of 7 decimal points.

Keeping in mind the memory double occupies with such precision, it has to be used more wisely.
Prefer to double over float when there is no space constraint or memory constraint and when more precision is needed.

If memory is a concern and/or precision to an extent of 16 decimals is not needed, it is advised to go with the float.

Recap : Difference between float and double in Java



floatdouble
Memory32 bits or 4 bytes64 bits or 8 bytes
Suffix'f' or 'F''d' or 'D'
Precision6-7 digits decimal points precision15-16 digits decimal points precision
Data LossNo data loss when trying to convert float to doubleData loss will occur if trying to convert double to float.
Default DatatypeNoYes
Wrapper classjava.lang.Floatjava.lang.Double
Default Value0.0f0.0d

That's all for today, we have learned 7 important differences between float and double in java. Please mention in the comments if you have any questions or doubts related to this article.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry