» » Difference between float and double java

  • Float data type in java is represented in 32 bits, with 1 sign bit, 8 bits of exponent, and 23 bits of the mantissa
  • Where as Double is  is represented in 64 bits, with 1 sign bit, 11 bits of exponent, and 52 bits of mantissa.
  • Default value of float is 0.0f.
  • Default value of double is 0.0d.
  • Floating points numbers also known as real numbers and in java there are two types of floating point one is float and another one is double.

  • Float specifies single precision and double specifies double precision.
  • According to the IEEE standards, float is a 32 bit representation of a real number while double is a 64 bit representation
  • Normally we use double instead of float to avoid common overflow of range of numbers
  • Check below diagram for width and range of float and double data types

float vs double java

 When do you use float and when do you use double:

  • Use double data type for all your calculations and temp variables. 
  • Use float when you need to maintain an array of numbers - float[] array (if precision is sufficient), and you are dealing with over tens of thousands of float numbers.
  • Most of the math functions or operators convert/return double, and you don't want to cast the numbers back to float for any intermediate steps.

How many significant digits have floats and doubles in java?

  • In java float can handle about 7 decimal places.
  • And double can handle about 16 decimal places

Program #1: write a java example program which explains differences between float and double in java

  1. package com.instanceofjava.floatvsdouble;

  2. import java.math.BigDecimal;

  3. public class FloatVsDouble {

  4. /**
  5. * @website: www.instanceofjava.com
  6. * @category: float vs double in java with example program
  7. */
  8. public static void main(String[] args) {
  9.     float  a=10.8632667283322234f;
  10.     double b=10.8632667283322234f;
  12.      System.out.println("float value="+a);
  13.      System.out.println("double value="+b);
  15.      b=10.8632667283322234d;
  17.     System.out.println("float value="+a);
  18.     System.out.println("double value="+b);

  20. }

  21. }


  1. float value=10.863267
  2. double value=10.863266944885254
  3. float value=10.863267
  4. double value=10.863266728332224

Difference between float and double java

Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu