Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Types and Literals

The float and double types

Reference for Java float and double: IEEE 754 floating-point numbers, precision limits, NaN and infinity, comparing safely and when to use BigDecimal.

double is a 64-bit IEEE 754 binary floating-point number with about 15 to 17 significant decimal digits. float is the 32-bit version with about 6 to 9 digits. A literal with a decimal point or exponent, such as 3.14 or 1e-3, is a double; add f for a float (3.14f).

Floating-point numbers store binary fractions, so many decimal values, 0.1 among them, cannot be represented exactly. Never use them for money or for exact equality tests. They also have special values: positive and negative infinity, NaN (not a number) and negative zero.

Java
double ratio = 2.0 / 3;
float small = 1.5f;
double sci = 6.022e23;

Try it

Java Precision and special values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Limits, formatting and BigDecimal

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
double d = 1.5;
double d2 = 10;       // int is widened
float f = 1.5f;       // the suffix is required
Type Size Precision Approximate maximum Wrapper
float 32 bits 6-9 digits Float.MAX_VALUE (3.4e38) Float
double 64 bits 15-17 digits Double.MAX_VALUE (1.8e308) Double

Default value of both is 0.0. Constants on the wrappers include POSITIVE_INFINITY, NEGATIVE_INFINITY, NaN and MIN_VALUE (the smallest positive value, not the most negative).

Usage notes

  • Default to double. It is faster to reason about and its extra precision costs almost nothing. Use float only for large arrays, graphics or APIs that require it.
  • Compare with a tolerance, or use Math.abs(a - b) < epsilon. Never use == on computed results.
  • NaN is never equal to anything, including itself. Test with Double.isNaN(). Double.equals() and Double.compare() treat NaN as equal to itself and order -0.0 before 0.0.
  • Division by zero does not throw for floating point; it yields infinity or NaN. Only integer division throws.
  • Money belongs in BigDecimal or in integer cents.
  • Casting to an integer type truncates the fraction and saturates at the target's limits; NaN becomes 0. Use Math.round(), floor() or ceil() to choose the rounding.
  • Results are deterministic. Since Java 17 all floating-point arithmetic follows strict IEEE 754 semantics (strictfp is redundant).
  • Mixed arithmetic. An int combined with a double becomes double; a long combined with a float becomes float, which loses precision.