Java float Vs double Type: What's the difference?

In this tutorial, we will learn about the float and double data types in Java and what's the differences between them. By Preeti Jain Last updated : January 02, 2024

Java 'float' Data Type

The float is a data type (or in other words, it is a keyword that has special meaning) in Java. The float takes 4 bytes (i.e. 32 bits) in memory (or in other words, we can represent (+,-) 3.40282347E + 38F Numbers). The float is a single precision floating point decimal number (i.e. we can represent 7 places of decimal accuracy, or in simple terms we can take 7 digits after decimal).

Java 'float' Type Example: Printing Size

public class Main {
  public static void main(String[] args) {
    float a;
    System.out.println(Float.SIZE / 8 + " BYTES");
  }
}

Output

4 BYTES

Java 'float' Type Example: Printing Precision

public class Main {
  public static void main(String[] args) {
    float a = 2.3456784f;
    System.out.println("Float Precision after decimal is " + a);
  }
}

Output:

Float Precision after decimal is 2.3456783

Java 'double' Data Type

The double is a data type (or in other words, it is a keyword that has special meaning) in Java. The double takes 8 bytes (i.e. 64 bits) in memory (we can represent (+,-)±1.79769313486231570E+308 Numbers).

The double is a double precision floating point decimal number (i.e. we can represent 15 places of decimal accuracy or in simple terms we can take 15 digits after decimal).

Java 'double' Type Example: Printing Size

public class Main {
  public static void main(String[] args) {
    double a;
    System.out.println(Double.SIZE / 8 + " BYTES");
  }
}

Output

8 BYTES

Java 'double' Type Example: Printing Precision

public class Main {
  public static void main(String[] args) {
    double a = 2.345678432433564;
    System.out.println("Double Precision after decimal is " + a);
  }
}

Output

Double Precision after decimal is 2.345678432433564

Difference between double and float Java types

Comparison Java float Type Java double Type
Precision The float type represents single-precision. The double type represents double-precision.
Storage Capacity The float type takes 4 bytes (32 bits) in memory. The double type takes 8 bytes (64 bits) in memory.
Default Value The default value of the float type is 0.0f The default value of the double type is 0.0d
Value Literal The 'f' literal is used with the float literal. The 'd' literal is used with the double literal.
Value Range The float type's value range is 3.4e-038 to 3.4e+038. The double type's value range is 1.7e-308 to 1.7e+308.
Wrapper Class The wrapper class of float type is java.lang.Float The wrapper class of double type is java.lang.Double

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.