Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท java.lang

The Double class

Reference for java.lang.Double: NaN, infinity, precision, parseDouble, isNaN, compare, equals versus ==, negative zero and floating-point pitfalls.

Double wraps the 64-bit IEEE 754 floating-point primitive double and provides constants, parsing, comparison and bit-level helpers. Floating-point numbers can represent huge ranges (about 1.8 x 10^308) but only about 15 to 17 significant decimal digits, and most decimal fractions such as 0.1 cannot be stored exactly.

Because of that, double has special values with no integer equivalent: positive and negative infinity, NaN (not a number), and negative zero. Understanding how these behave in comparisons, equals and collections explains most surprises.

Java
double d = 0.1 + 0.2;          // 0.30000000000000004
Double boxed = d;
boolean bad = Double.isNaN(0.0 / 0.0);   // true

Try it

Java Special values and precision

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 equals, compare and negative zero

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
public final class Double extends Number implements Comparable<Double>
Member Description
MAX_VALUE Largest finite value, about 1.8e308.
MIN_VALUE Smallest positive value, 4.9e-324 (not the most negative number).
POSITIVE_INFINITY, NEGATIVE_INFINITY, NaN The special constants.
SIZE, BYTES 64 and 8.
parseDouble(String), valueOf(String) Text to double / Double; accepts exponents, NaN, Infinity; trims whitespace.
isNaN(double), isInfinite(double), isFinite(double) Classification.
compare(double, double), compareTo(Double) Total ordering: -0.0 < 0.0, NaN is greatest.
doubleToLongBits, longBitsToDouble The IEEE 754 bit pattern.
sum, max, min Method references for streams.
toString(double), toHexString(double) Shortest round-trip decimal text, or hexadecimal floating-point text.

Throws: NumberFormatException from parseDouble for non-numeric text; NullPointerException for a null string or when unboxing null.

Usage notes

  • Never compare with == after arithmetic. Compare with a tolerance (Math.abs(a - b) < eps), or use BigDecimal for decimal values such as money.
  • NaN is not equal to itself with ==, < or >. Use Double.isNaN(x). Boxed Double.equals treats NaN as equal to NaN, which is what makes Double safe as a map key.
  • == and equals disagree on zeros. 0.0 == -0.0 is true, but Double.valueOf(0.0).equals(-0.0) is false. Double.compare also puts -0.0 before 0.0.
  • Division by zero does not throw. 1.0 / 0 is Infinity; only integer division throws ArithmeticException.
  • Casting to int truncates toward zero, turns NaN into 0 and clamps out-of-range values to Integer.MAX_VALUE or MIN_VALUE.
  • MIN_VALUE is tiny and positive. To get the most negative double use -Double.MAX_VALUE.
  • Parsing is locale-independent and expects a dot as decimal separator. "1,5" throws; use NumberFormat for localized text.
  • Boxing. Double objects are never cached, so == on two boxed values is almost always false. Use equals or unbox first.
  • Streams. DoubleStream.sum() uses compensated summation and can differ slightly from a plain loop.