< 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

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

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

  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

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