Package java.lang

Class Double

All Implemented Interfaces:
Serializable, Comparable<Double>

public final class Double
extends Number
implements Comparable<Double>
The wrapper for the primitive type double.
Since:
1.0
See Also:
Number, Serialized Form
  • Field Summary

    Fields
    Modifier and Type Field Description
    static int MAX_EXPONENT
    Maximum base-2 exponent that a finite value of the double type may have.
    static double MAX_VALUE
    Constant for the maximum double value, (2 - 2-52) * 21023.
    static int MIN_EXPONENT
    Minimum base-2 exponent that a normal value of the double type may have.
    static double MIN_NORMAL
    Constant for the smallest positive normal value of the double type.
    static double MIN_VALUE
    Constant for the minimum double value, 2-1074.
    static double NaN
    Constant for the Not-a-Number (NaN) value of the double type.
    static double NEGATIVE_INFINITY
    Constant for the negative infinity value of the double type.
    static double POSITIVE_INFINITY
    Constant for the positive infinity value of the double type.
    static int SIZE
    Constant for the number of bits needed to represent a double in two's complement form.
    static Class<Double> TYPE
    The Class object that represents the primitive type double.
  • Constructor Summary

    Constructors
    Constructor Description
    Double​(double value)
    Constructs a new Double with the specified primitive double value.
    Double​(String string)
    Constructs a new Double from the specified string.
  • Method Summary

    Modifier and Type Method Description
    byte byteValue()
    Returns this object's value as a byte.
    static int compare​(double double1, double double2)
    Compares the two specified double values.
    int compareTo​(Double object)
    Compares this object to the specified double object to determine their relative order.
    static long doubleToLongBits​(double value)
    Returns an integer corresponding to the bits of the given IEEE 754 double precision value.
    static long doubleToRawLongBits​(double value)
    Returns an integer corresponding to the bits of the given IEEE 754 double precision value.
    double doubleValue()
    Gets the primitive value of this double.
    boolean equals​(Object object)
    Tests this double for equality with object.
    float floatValue()
    Returns this object's value as a float.
    int hashCode()
    Returns an integer hash code for this object.
    int intValue()
    Returns this object's value as an int.
    boolean isInfinite()
    Indicates whether this object represents an infinite value.
    static boolean isInfinite​(double d)
    Indicates whether the specified double represents an infinite value.
    boolean isNaN()
    Indicates whether this object is a Not-a-Number (NaN) value.
    static boolean isNaN​(double d)
    Indicates whether the specified double is a Not-a-Number (NaN) value.
    static double longBitsToDouble​(long bits)
    Returns the IEEE 754 double precision float corresponding to the given bits.
    long longValue()
    Returns this object's value as a long.
    static double parseDouble​(String string)
    Parses the specified string as a double value.
    short shortValue()
    Returns this object's value as a short.
    static String toHexString​(double d)
    Converts the specified double into its hexadecimal string representation.
    String toString()
    Returns a string containing a concise, human-readable description of this object.
    static String toString​(double d)
    Returns a string containing a concise, human-readable description of the specified double value.
    static Double valueOf​(double d)
    Returns a Double instance for the specified double value.
    static Double valueOf​(String string)
    Parses the specified string as a double value.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAX_VALUE

      public static final double MAX_VALUE
      Constant for the maximum double value, (2 - 2-52) * 21023.
      See Also:
      Constant Field Values
    • MIN_VALUE

      public static final double MIN_VALUE
      Constant for the minimum double value, 2-1074.
      See Also:
      Constant Field Values
    • NaN

      public static final double NaN
      Constant for the Not-a-Number (NaN) value of the double type.
      See Also:
      Constant Field Values
    • POSITIVE_INFINITY

      public static final double POSITIVE_INFINITY
      Constant for the positive infinity value of the double type.
      See Also:
      Constant Field Values
    • NEGATIVE_INFINITY

      public static final double NEGATIVE_INFINITY
      Constant for the negative infinity value of the double type.
      See Also:
      Constant Field Values
    • MIN_NORMAL

      public static final double MIN_NORMAL
      Constant for the smallest positive normal value of the double type.
      Since:
      1.6
      See Also:
      Constant Field Values
    • MAX_EXPONENT

      public static final int MAX_EXPONENT
      Maximum base-2 exponent that a finite value of the double type may have. Equal to Math.getExponent(Double.MAX_VALUE).
      Since:
      1.6
      See Also:
      Constant Field Values
    • MIN_EXPONENT

      public static final int MIN_EXPONENT
      Minimum base-2 exponent that a normal value of the double type may have. Equal to Math.getExponent(Double.MIN_NORMAL).
      Since:
      1.6
      See Also:
      Constant Field Values
    • TYPE

      public static final Class<Double> TYPE
      The Class object that represents the primitive type double.
      Since:
      1.1
    • SIZE

      public static final int SIZE
      Constant for the number of bits needed to represent a double in two's complement form.
      Since:
      1.5
      See Also:
      Constant Field Values
  • Constructor Details

    • Double

      public Double​(double value)
      Constructs a new Double with the specified primitive double value.
      Parameters:
      value - the primitive double value to store in the new instance.
    • Double

      public Double​(String string) throws NumberFormatException
      Constructs a new Double from the specified string.
      Parameters:
      string - the string representation of a double value.
      Throws:
      NumberFormatException - if string cannot be parsed as a double value.
      See Also:
      parseDouble(String)
  • Method Details

    • compareTo

      public int compareTo​(Double object)
      Compares this object to the specified double object to determine their relative order. There are two special cases:
      • Double.NaN is equal to Double.NaN and it is greater than any other double value, including Double.POSITIVE_INFINITY;
      • +0.0d is greater than -0.0d
      Specified by:
      compareTo in interface Comparable<Double>
      Parameters:
      object - the double object to compare this object to.
      Returns:
      a negative value if the value of this double is less than the value of object; 0 if the value of this double and the value of object are equal; a positive value if the value of this double is greater than the value of object.
      Throws:
      NullPointerException - if object is null.
      Since:
      1.2
      See Also:
      Comparable
    • byteValue

      public byte byteValue()
      Description copied from class: Number
      Returns this object's value as a byte. Might involve rounding and/or truncating the value, so it fits into a byte.
      Overrides:
      byteValue in class Number
      Returns:
      the primitive byte value of this object.
    • doubleToLongBits

      public static long doubleToLongBits​(double value)
      Returns an integer corresponding to the bits of the given IEEE 754 double precision value. All Not-a-Number (NaN) values are converted to a single NaN representation (0x7ff8000000000000L) (compare to doubleToRawLongBits(double)).
    • doubleToRawLongBits

      public static long doubleToRawLongBits​(double value)
      Returns an integer corresponding to the bits of the given IEEE 754 double precision value. Not-a-Number (NaN) values are preserved (compare to doubleToLongBits(double)).
    • doubleValue

      public double doubleValue()
      Gets the primitive value of this double.
      Specified by:
      doubleValue in class Number
      Returns:
      this object's primitive value.
    • equals

      public boolean equals​(Object object)
      Tests this double for equality with object. To be equal, object must be an instance of Double and doubleToLongBits must give the same value for both objects.

      Note that, unlike ==, -0.0 and +0.0 compare unequal, and NaNs compare equal by this method.

      Overrides:
      equals in class Object
      Parameters:
      object - the object to compare this double with.
      Returns:
      true if the specified object is equal to this Double; false otherwise.
      See Also:
      Object.hashCode()
    • floatValue

      public float floatValue()
      Description copied from class: Number
      Returns this object's value as a float. Might involve rounding.
      Specified by:
      floatValue in class Number
      Returns:
      the primitive float value of this object.
    • hashCode

      public int hashCode()
      Description copied from class: Object
      Returns an integer hash code for this object. By contract, any two objects for which Object.equals(java.lang.Object) returns true must return the same hash code value. This means that subclasses of Object usually override both methods or neither method.

      Note that hash values must not change over time unless information used in equals comparisons also changes.

      See Writing a correct hashCode method if you intend implementing your own hashCode method.

      Overrides:
      hashCode in class Object
      Returns:
      this object's hash code.
      See Also:
      Object.equals(java.lang.Object)
    • intValue

      public int intValue()
      Description copied from class: Number
      Returns this object's value as an int. Might involve rounding and/or truncating the value, so it fits into an int.
      Specified by:
      intValue in class Number
      Returns:
      the primitive int value of this object.
    • isInfinite

      public boolean isInfinite()
      Indicates whether this object represents an infinite value.
      Returns:
      true if the value of this double is positive or negative infinity; false otherwise.
    • isInfinite

      public static boolean isInfinite​(double d)
      Indicates whether the specified double represents an infinite value.
      Parameters:
      d - the double to check.
      Returns:
      true if the value of d is positive or negative infinity; false otherwise.
    • isNaN

      public boolean isNaN()
      Indicates whether this object is a Not-a-Number (NaN) value.
      Returns:
      true if this double is Not-a-Number; false if it is a (potentially infinite) double number.
    • isNaN

      public static boolean isNaN​(double d)
      Indicates whether the specified double is a Not-a-Number (NaN) value.
      Parameters:
      d - the double value to check.
      Returns:
      true if d is Not-a-Number; false if it is a (potentially infinite) double number.
    • longBitsToDouble

      public static double longBitsToDouble​(long bits)
      Returns the IEEE 754 double precision float corresponding to the given bits.
    • longValue

      public long longValue()
      Description copied from class: Number
      Returns this object's value as a long. Might involve rounding and/or truncating the value, so it fits into a long.
      Specified by:
      longValue in class Number
      Returns:
      the primitive long value of this object.
    • parseDouble

      public static double parseDouble​(String string) throws NumberFormatException
      Parses the specified string as a double value.
      Parameters:
      string - the string representation of a double value.
      Returns:
      the primitive double value represented by string.
      Throws:
      NumberFormatException - if string cannot be parsed as a double value.
    • shortValue

      public short shortValue()
      Description copied from class: Number
      Returns this object's value as a short. Might involve rounding and/or truncating the value, so it fits into a short.
      Overrides:
      shortValue in class Number
      Returns:
      the primitive short value of this object.
    • toString

      public String toString()
      Description copied from class: Object
      Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
         getClass().getName() + '@' + Integer.toHexString(hashCode())

      See Writing a useful toString method if you intend implementing your own toString method.

      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.
    • toString

      public static String toString​(double d)
      Returns a string containing a concise, human-readable description of the specified double value.
      Parameters:
      d - the double to convert to a string.
      Returns:
      a printable representation of d.
    • valueOf

      public static Double valueOf​(String string) throws NumberFormatException
      Parses the specified string as a double value.
      Parameters:
      string - the string representation of a double value.
      Returns:
      a Double instance containing the double value represented by string.
      Throws:
      NumberFormatException - if string cannot be parsed as a double value.
      See Also:
      parseDouble(String)
    • compare

      public static int compare​(double double1, double double2)
      Compares the two specified double values. There are two special cases:
      • Double.NaN is equal to Double.NaN and it is greater than any other double value, including Double.POSITIVE_INFINITY;
      • +0.0d is greater than -0.0d
      Parameters:
      double1 - the first value to compare.
      double2 - the second value to compare.
      Returns:
      a negative value if double1 is less than double2; 0 if double1 and double2 are equal; a positive value if double1 is greater than double2.
    • valueOf

      public static Double valueOf​(double d)
      Returns a Double instance for the specified double value.
      Parameters:
      d - the double value to store in the instance.
      Returns:
      a Double instance containing d.
      Since:
      1.5
    • toHexString

      public static String toHexString​(double d)
      Converts the specified double into its hexadecimal string representation.
      Parameters:
      d - the double to convert.
      Returns:
      the hexadecimal string representation of d.
      Since:
      1.5