Double (Java SE 17 & JDK 17) (original) (raw)

All Implemented Interfaces:

[Serializable](../io/Serializable.html "interface in java.io"), [Comparable](Comparable.html "interface in java.lang")<[Double](Double.html "class in java.lang")>, [Constable](constant/Constable.html "interface in java.lang.constant"), [ConstantDesc](constant/ConstantDesc.html "interface in java.lang.constant")


The Double class wraps a value of the primitive typedouble in an object. An object of typeDouble contains a single field whose type isdouble.

In addition, this class provides several methods for converting adouble to a String and aString to a double, as well as other constants and methods useful when dealing with adouble.

This is a value-based class; programmers should treat instances that areequal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.

Floating-point Equality, Equivalence, and Comparison

IEEE 754 floating-point values include finite nonzero values, signed zeros (+0.0 and -0.0), signed infinitiespositive infinity andnegative infinity), andNaN (not-a-number).

An equivalence relation on a set of values is a boolean relation on pairs of values that is reflexive, symmetric, and transitive. For more discussion of equivalence relations and object equality, see the Object.equals specification. An equivalence relation partitions the values it operates over into sets called equivalence classes. All the members of the equivalence class are equal to each other under the relation. An equivalence class may contain only a single member. At least for some purposes, all the members of an equivalence class are substitutable for each other. In particular, in a numeric expression equivalent values can be substituted for one another without changing the result of the expression, meaning changing the equivalence class of the result of the expression.

Notably, the built-in == operation on floating-point values is not an equivalence relation. Despite not defining an equivalence relation, the semantics of the IEEE 754== operator were deliberately designed to meet other needs of numerical computation. There are two exceptions where the properties of an equivalence relation are not satisfied by == on floating-point values:

For ordered comparisons using the built-in comparison operators (<, <=, etc.), NaN values have another anomalous situation: a NaN is neither less than, nor greater than, nor equal to any value, including itself. This means the trichotomy of comparison does not hold.

To provide the appropriate semantics for equals andcompareTo methods, those methods cannot simply be wrappers around == or ordered comparison operations. Instead, equals defines NaN arguments to be equal to each other and defines +0.0 to not be equal to -0.0, restoring reflexivity. For comparisons, compareTo defines a total order where -0.0 is less than +0.0 and where a NaN is equal to itself and considered greater than positive infinity.

The operational semantics of equals and compareTo are expressed in terms of bit-wise converting the floating-point values to integral values.

The natural ordering implemented by compareTo is consistent with equals. That is, two objects are reported as equal by equals if and only if compareTo on those objects returns zero.

The adjusted behaviors defined for equals and compareTo allow instances of wrapper classes to work properly with conventional data structures. For example, defining NaN values to be equals to one another allows NaN to be used as an element of a HashSet or as the key of a HashMap. Similarly, defining compareTo as a total ordering, including +0.0, -0.0, and NaN, allows instances of wrapper classes to be used as elements of a SortedSet or as keys of aSortedMap.

See Java Language Specification:

4.2.3 Floating-Point Types, Formats, and Values
4.2.4. Floating-Point Operations
15.21.1 Numerical Equality Operators == and !=
15.20.1 Numerical Comparison Operators <, <=, >, and >=

Since:

1.0

See Also:

Fields
static final int
The number of bytes used to represent a double value.
static final int
Maximum exponent a finite double variable may have.
static final double
A constant holding the largest positive finite value of typedouble, (2-2-52)·21023.
static final int
Minimum exponent a normalized double variable may have.
static final double
A constant holding the smallest positive normal value of typedouble, 2-1022.
static final double
A constant holding the smallest positive nonzero value of typedouble, 2-1074.
static final double
A constant holding a Not-a-Number (NaN) value of typedouble.
static final double
A constant holding the negative infinity of typedouble.
static final double
A constant holding the positive infinity of typedouble.
static final int
The number of bits used to represent a double value.
The Class instance representing the primitive typedouble.

Constructors
[Double](#%3Cinit%3E%28double%29)(double value)
Deprecated, for removal: This API element is subject to removal in a future version.
Deprecated, for removal: This API element is subject to removal in a future version.

byte
[byteValue](#byteValue%28%29)()
Returns the value of this Double as a byte after a narrowing primitive conversion.
static int
[compare](#compare%28double,double%29)(double d1, double d2)
Compares the two specified double values.
int
Compares two Double objects numerically.
Returns an Optional containing the nominal descriptor for this instance, which is the instance itself.
static long
[doubleToLongBits](#doubleToLongBits%28double%29)(double value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout.
static long
[doubleToRawLongBits](#doubleToRawLongBits%28double%29)(double value)
Returns a representation of the specified floating-point value according to the IEEE 754 floating-point "double format" bit layout, preserving Not-a-Number (NaN) values.
double
Returns the double value of this Double object.
boolean
Compares this object against the specified object.
float
Returns the value of this Double as a float after a narrowing primitive conversion.
int
[hashCode](#hashCode%28%29)()
Returns a hash code for this Double object.
static int
[hashCode](#hashCode%28double%29)(double value)
Returns a hash code for a double value; compatible withDouble.hashCode().
int
[intValue](#intValue%28%29)()
Returns the value of this Double as an int after a narrowing primitive conversion.
static boolean
[isFinite](#isFinite%28double%29)(double d)
Returns true if the argument is a finite floating-point value; returns false otherwise (for NaN and infinity arguments).
boolean
Returns true if this Double value is infinitely large in magnitude, false otherwise.
static boolean
[isInfinite](#isInfinite%28double%29)(double v)
Returns true if the specified number is infinitely large in magnitude, false otherwise.
boolean
[isNaN](#isNaN%28%29)()
Returns true if this Double value is a Not-a-Number (NaN), false otherwise.
static boolean
[isNaN](#isNaN%28double%29)(double v)
Returns true if the specified number is a Not-a-Number (NaN) value, false otherwise.
static double
[longBitsToDouble](#longBitsToDouble%28long%29)(long bits)
Returns the double value corresponding to a given bit representation.
long
[longValue](#longValue%28%29)()
Returns the value of this Double as a long after a narrowing primitive conversion.
static double
[max](#max%28double,double%29)(double a, double b)
Returns the greater of two double values as if by calling Math.max.
static double
[min](#min%28double,double%29)(double a, double b)
Returns the smaller of two double values as if by calling Math.min.
static double
Returns a new double initialized to the value represented by the specified String, as performed by the valueOf method of classDouble.
Resolves this instance as a ConstantDesc, the result of which is the instance itself.
short
Returns the value of this Double as a short after a narrowing primitive conversion.
static double
[sum](#sum%28double,double%29)(double a, double b)
Adds two double values together as per the + operator.
[toHexString](#toHexString%28double%29)(double d)
Returns a hexadecimal string representation of thedouble argument.
[toString](#toString%28%29)()
Returns a string representation of this Double object.
[toString](#toString%28double%29)(double d)
Returns a string representation of the double argument.
[valueOf](#valueOf%28double%29)(double d)
Returns a Double instance representing the specifieddouble value.
Returns a Double object holding thedouble value represented by the argument strings.