Float and Double in Java

The byte, short, int, and long data types are used to store the integer values in Java programming, but the float and double data types used to store real or floating-point numbers. Prerequisites:- Data types in Java programming

Double data type in Java

Size :: 8 bytes
Range :: -3.4e38 to 3.4e38
          Or, 
         4.94065645841246544e-324
         to 1.79769313486231570e+308
Default value :: 0.0
Corresponding Wrapper class :: Double

The suffix ‘D’ or ‘d’ is optional for the double data type.

double d2 = 95.5; // valid
double d3 = 95.5D; // valid
double d4 = 95.5d; // valid

Except boolean, the double data type can hold all other primitive data type values.

public class Test {
  public static void main(String[] args) {
     double d1 = (byte)10; // byte
     double d2 = (short)20; // short
     double d3 = 30; // int
     double d4 = 40L; // long
     double d5 = 'a'; // char
     double d6 = 97.5; // double
     double d7 = 95.0F; // float
     // double d8 = true; // error

     // display values
     System.out.println(d1); // 10.0
     System.out.println(d2); // 20.0
     System.out.println(d3); // 30.0
     System.out.println(d4); // 40.0
     System.out.println(d5); // 97.0
     System.out.println(d6); // 97.5
     System.out.println(d7); // 95.0
  }
}

In Java, by default, every floating-point number is of the double data type. When we use floating-point numbers inside the program then the compiler and JVM treat them as a double data type.

// program to show, by default every floating-point 
// number is of the double data type
class Test{
   public static void main(String[] args) {
      m1(1.9);
   }
   static void m1(float n){
      System.out.println("float parameter.");
   }
   static void m1(double n){
      System.out.println("double parameter.");
   }
}

Output:-

double parameter

In the above program, we use the method overloading concept (We will discuss it later, At this time don’t think much about method overloading, it is used here just to prove the concept). From the main method we call a function m1() and method m1 has value 1.9. By default, every floating-point number is of the double data type. So, Compiler and JVM considered the number 1.9 as the double data type. Hence, the method having parameter double will be executed.

Float data type in Java

Size :: 4 bytes
Range :: -1.7e38 to 1.7e38
         Or, 
         1.40129846432481707e-45
         to 3.40282346638528860e+38
Default value :: 0.0F
Corresponding Wrapper class :: Float

We must add suffix F or f to represent a floating-point number as a float data type and to store the floating-point number in a float variable.
float n1 = 9.9F;

Note:- We can assign floating point numbers directly to double data type but we can’t assign directly to float data type.
float n1 = 9.9; // error, we must use F or f suffix
double n2 = 9.9; // valid

In regular programming calculations, we don’t use float. If we ensure that the result range is within the range of float data type then we can choose a float data type for saving memory. Generally, we use double because of two reasons:-

  • If we want to use the floating-point number as float data type then method caller must explicitly suffix F or f, because by default every floating-point number is treated as double. It increases the burden to the programmer. If we use a floating-point number as double data type then we don’t need to add any suffix.
  • Float is a single-precision data type means it occupies 4 bytes. Hence in large computations, we will not get a complete result. If we choose double data type, it occupies 8 bytes and we will get complete results.
  • float is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the significand
  • double is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of significand.

Both float and double data types were designed especially for scientific calculations, where approximation errors are acceptable. If accuracy is the most prior concern then, it is recommended to use BigDecimal class instead of float or double data types.

Table

Data type Size (in byte) Default value Range
float 4 0.0F -1.7e38 to 1.7e38 Or,
1.40129846432481707e-45
to
3.40282346638528860e+38
double 8 0.0 -3.4e38 to 3.4e38 Or,
4.94065645841246544e-324
to
1.79769313486231570e+308

Comparison with C/C++ language

In C language we can store the floating-point number in either float or double variable, even we can store in int type variable but C compiler doesn’t throw an error because it will do required conversions.

Statement C language Java Language
int n1 = 9.9; Allowed Not allowed
float n2 = 9.9; Allowed Not allowed
double n3 = 9.9; Allowed Allowed

In Java, we can’t do the same thing. If we want to assign a floating-point number to a float variable then we must add suffix F or f. And if we want to assign to another variable then we must use a cast operator. Below all statements are valid.
short n1 = (short)9.0;
int n2 = (int)9.0;
float n3 = 9.0F;
float n4 = (float)9.0;
double n5 = 9.0;

Sample programs

Program1:- Write a Java program to demonstrate Floating-point numbers in Java.

class FloatingPointNumber {
  public static void main(String[] args) {
     double a = 9.5;
     // float b = 15.5; // error: incompatible types
     float c = 20.9F;

     System.out.println("Double = " + a);
     System.out.println("Float = " + c);
  }
}

Output:-

Double = 9.5
Float = 20.9

Program2:- Write a Java program to find the sum and average of three floating-point numbers.

class SumAverage {
  public static void main(String[] args) {
     double a = 9.5;
     double b = 15.5;
     double c = 20.9;

     double sum = a+b+c;
     double average = sum / 3;

     System.out.println("Sum = " + sum);
     System.out.println("Average = " + average);
  }
}

Output:-

Sum = 45.9
Average = 15.299999999999999

Difference between float and double in Java

The float data typeThe double data type
The size of the float data type is 4 bytes.The size of the double data type is 8 bytes.
It is single precision.It is double precision.
It stores 5 to 6 decimal place accuracy.It stores 14 to 15 decimal place accuracy.
The range is from -1.7e38 to 1.7e38The range is from -3.4e38 to 3.4e38
To represent a floating-point number as a float data type we must use the ‘F’ or ‘f’ suffix.By default, every floating-point number is of double type. The ‘D’ or ‘d’ are optional suffix.
The corresponding wrapper class is Float.The corresponding wrapper class is Double.

Precision differences

Different behaviors of storing the same value using float and double data type.

In Java, the float and double data types store same value for x.0, x.5 but it stores different values for x.1, x.2, x.3, x.4, x.6, x.7, x.8, and x.9 where x is any integer number.

public class Test {
  public static void main(String[] args) {
     double d1 = 1.0;
     float f1 = 1.0F;
     System.out.printf("%.15f \n", d1);
     System.out.printf("%.15f \n", f1);

     // compare d1 and f1
     if(d1 == f1)
     System.out.println("Both are same.");
     else
     System.out.println("Both are different.");

     double d2 = 1.1;
     float f2 = 1.1F;
     System.out.printf("%.15f \n", d2);
     System.out.printf("%.15f \n", f2);
      
     // compare d2 and f2
     if(d2 == f2)
     System.out.println("Both are same.");
     else
     System.out.println("Both are different.");
  }
}

Output:-

1.000000000000000
1.000000000000000
Both are same.
1.100000000000000
1.100000023841858
Both are different.

From the above program, we can observe that for 1.0 both double and float data type store the same value but for the 1.1 both stores different values. Let us see some more programs related to i.

Guess the output of the below two programs

class CheckDifference1 {
   public static void main(String[] args) {
      double a = 9.2;
      float b = 9.2F;

      if(a==b) System.out.println("a and b are equal");
      else System.out.println("a and b are different");

      System.out.printf("a = %.15f \n", a);
      System.out.printf("b = %.15f \n", b);

      if(9.2==9.2F) System.out.println("9.2 and 9.2F are equal");
      else System.out.println("9.2 and 9.2F are different");
   }
}

Output:-

a and b are different
a = 9.200000000000000
b = 9.199999809265137
9.2 and 9.2F are different

Note:- Due to these differences we should not use == operator with float and double data types to compare the values. Instead of using == operator, we should > and < comparison operators.


If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

2 thoughts on “Float and Double in Java”

  1. Jarred Thammavong

    Hi! Would you mind if I share your blog with my twitter group? There’s a lot of people that I think would really appreciate your content. Please let me know. Thanks

Leave a Reply