< prev index next >

src/java.base/share/classes/java/lang/Double.java

Print this page

  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.invoke.MethodHandles;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.util.Optional;
  32 
  33 import jdk.internal.math.FloatingDecimal;
  34 import jdk.internal.math.DoubleConsts;
  35 import jdk.internal.math.DoubleToDecimal;

  36 import jdk.internal.vm.annotation.IntrinsicCandidate;
  37 
  38 /**
  39  * The {@code Double} class is the {@linkplain
  40  * java.lang##wrapperClass wrapper class} for values of the primitive
  41  * type {@code double}. An object of type {@code Double} contains a
  42  * single field whose type is {@code double}.
  43  *
  44  * <p>In addition, this class provides several methods for converting a
  45  * {@code double} to a {@code String} and a
  46  * {@code String} to a {@code double}, as well as other
  47  * constants and methods useful when dealing with a
  48  * {@code double}.
  49  *
  50  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  51  * class; programmers should treat instances that are
  52  * {@linkplain #equals(Object) equal} as interchangeable and should not
  53  * use instances for synchronization, or unpredictable behavior may
  54  * occur. For example, in a future release, synchronization may fail.
  55  *

 340  *  } // Value of d approximately 1.0999999999999999
 341  *  }
 342  *
 343  * While floating-point arithmetic may have surprising results, IEEE
 344  * 754 floating-point arithmetic follows a principled design and its
 345  * behavior is predictable on the Java platform.
 346  *
 347  * @jls 4.2.3 Floating-Point Types and Values
 348  * @jls 4.2.4 Floating-Point Operations
 349  * @jls 15.21.1 Numerical Equality Operators == and !=
 350  * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
 351  *
 352  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
 353  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
 354  *
 355  * @author  Lee Boynton
 356  * @author  Arthur van Hoff
 357  * @author  Joseph D. Darcy
 358  * @since 1.0
 359  */

 360 @jdk.internal.ValueBased
 361 public final class Double extends Number
 362         implements Comparable<Double>, Constable, ConstantDesc {
 363     /**
 364      * A constant holding the positive infinity of type
 365      * {@code double}. It is equal to the value returned by
 366      * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
 367      */
 368     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
 369 
 370     /**
 371      * A constant holding the negative infinity of type
 372      * {@code double}. It is equal to the value returned by
 373      * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
 374      */
 375     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
 376 
 377     /**
 378      * A constant holding a Not-a-Number (NaN) value of type
 379      * {@code double}. It is equivalent to the value returned by

 931      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 932      */
 933     public static Double valueOf(String s) throws NumberFormatException {
 934         return new Double(parseDouble(s));
 935     }
 936 
 937     /**
 938      * Returns a {@code Double} instance representing the specified
 939      * {@code double} value.
 940      * If a new {@code Double} instance is not required, this method
 941      * should generally be used in preference to the constructor
 942      * {@link #Double(double)}, as this method is likely to yield
 943      * significantly better space and time performance by caching
 944      * frequently requested values.
 945      *
 946      * @param  d a double value.
 947      * @return a {@code Double} instance representing {@code d}.
 948      * @since  1.5
 949      */
 950     @IntrinsicCandidate

 951     public static Double valueOf(double d) {
 952         return new Double(d);
 953     }
 954 
 955     /**
 956      * Returns a new {@code double} initialized to the value
 957      * represented by the specified {@code String}, as performed
 958      * by the {@code valueOf} method of class
 959      * {@code Double}.
 960      *
 961      * @param  s   the string to be parsed.
 962      * @return the {@code double} value represented by the string
 963      *         argument.
 964      * @throws NullPointerException  if the string is null
 965      * @throws NumberFormatException if the string does not contain
 966      *         a parsable {@code double}.
 967      * @see    java.lang.Double#valueOf(String)
 968      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 969      * @since 1.2
 970      */

  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.lang;
  27 
  28 import java.lang.invoke.MethodHandles;
  29 import java.lang.constant.Constable;
  30 import java.lang.constant.ConstantDesc;
  31 import java.util.Optional;
  32 
  33 import jdk.internal.math.FloatingDecimal;
  34 import jdk.internal.math.DoubleConsts;
  35 import jdk.internal.math.DoubleToDecimal;
  36 import jdk.internal.value.DeserializeConstructor;
  37 import jdk.internal.vm.annotation.IntrinsicCandidate;
  38 
  39 /**
  40  * The {@code Double} class is the {@linkplain
  41  * java.lang##wrapperClass wrapper class} for values of the primitive
  42  * type {@code double}. An object of type {@code Double} contains a
  43  * single field whose type is {@code double}.
  44  *
  45  * <p>In addition, this class provides several methods for converting a
  46  * {@code double} to a {@code String} and a
  47  * {@code String} to a {@code double}, as well as other
  48  * constants and methods useful when dealing with a
  49  * {@code double}.
  50  *
  51  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  52  * class; programmers should treat instances that are
  53  * {@linkplain #equals(Object) equal} as interchangeable and should not
  54  * use instances for synchronization, or unpredictable behavior may
  55  * occur. For example, in a future release, synchronization may fail.
  56  *

 341  *  } // Value of d approximately 1.0999999999999999
 342  *  }
 343  *
 344  * While floating-point arithmetic may have surprising results, IEEE
 345  * 754 floating-point arithmetic follows a principled design and its
 346  * behavior is predictable on the Java platform.
 347  *
 348  * @jls 4.2.3 Floating-Point Types and Values
 349  * @jls 4.2.4 Floating-Point Operations
 350  * @jls 15.21.1 Numerical Equality Operators == and !=
 351  * @jls 15.20.1 Numerical Comparison Operators {@code <}, {@code <=}, {@code >}, and {@code >=}
 352  *
 353  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
 354  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
 355  *
 356  * @author  Lee Boynton
 357  * @author  Arthur van Hoff
 358  * @author  Joseph D. Darcy
 359  * @since 1.0
 360  */
 361 @jdk.internal.MigratedValueClass
 362 @jdk.internal.ValueBased
 363 public final class Double extends Number
 364         implements Comparable<Double>, Constable, ConstantDesc {
 365     /**
 366      * A constant holding the positive infinity of type
 367      * {@code double}. It is equal to the value returned by
 368      * {@code Double.longBitsToDouble(0x7ff0000000000000L)}.
 369      */
 370     public static final double POSITIVE_INFINITY = 1.0 / 0.0;
 371 
 372     /**
 373      * A constant holding the negative infinity of type
 374      * {@code double}. It is equal to the value returned by
 375      * {@code Double.longBitsToDouble(0xfff0000000000000L)}.
 376      */
 377     public static final double NEGATIVE_INFINITY = -1.0 / 0.0;
 378 
 379     /**
 380      * A constant holding a Not-a-Number (NaN) value of type
 381      * {@code double}. It is equivalent to the value returned by

 933      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 934      */
 935     public static Double valueOf(String s) throws NumberFormatException {
 936         return new Double(parseDouble(s));
 937     }
 938 
 939     /**
 940      * Returns a {@code Double} instance representing the specified
 941      * {@code double} value.
 942      * If a new {@code Double} instance is not required, this method
 943      * should generally be used in preference to the constructor
 944      * {@link #Double(double)}, as this method is likely to yield
 945      * significantly better space and time performance by caching
 946      * frequently requested values.
 947      *
 948      * @param  d a double value.
 949      * @return a {@code Double} instance representing {@code d}.
 950      * @since  1.5
 951      */
 952     @IntrinsicCandidate
 953     @DeserializeConstructor
 954     public static Double valueOf(double d) {
 955         return new Double(d);
 956     }
 957 
 958     /**
 959      * Returns a new {@code double} initialized to the value
 960      * represented by the specified {@code String}, as performed
 961      * by the {@code valueOf} method of class
 962      * {@code Double}.
 963      *
 964      * @param  s   the string to be parsed.
 965      * @return the {@code double} value represented by the string
 966      *         argument.
 967      * @throws NullPointerException  if the string is null
 968      * @throws NumberFormatException if the string does not contain
 969      *         a parsable {@code double}.
 970      * @see    java.lang.Double#valueOf(String)
 971      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 972      * @since 1.2
 973      */
< prev index next >