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.
double d = 0.1 + 0.2; // 0.30000000000000004 Double boxed = d; boolean bad = Double.isNaN(0.0 / 0.0); // true
Try it
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.
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
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 useBigDecimalfor decimal values such as money. NaNis not equal to itself with==,<or>. UseDouble.isNaN(x). BoxedDouble.equalstreatsNaNas equal toNaN, which is what makesDoublesafe as a map key.==andequalsdisagree on zeros.0.0 == -0.0istrue, butDouble.valueOf(0.0).equals(-0.0)isfalse.Double.comparealso puts-0.0before0.0.- Division by zero does not throw.
1.0 / 0isInfinity; only integer division throwsArithmeticException. - Casting to
inttruncates toward zero, turnsNaNinto0and clamps out-of-range values toInteger.MAX_VALUEorMIN_VALUE. MIN_VALUEis 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; useNumberFormatfor localized text. - Boxing.
Doubleobjects are never cached, so==on two boxed values is almost alwaysfalse. Useequalsor unbox first. - Streams.
DoubleStream.sum()uses compensated summation and can differ slightly from a plain loop.