< prev index next >

src/java.base/share/classes/java/lang/Float.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.FloatConsts;
  34 import jdk.internal.math.FloatingDecimal;
  35 import jdk.internal.math.FloatToDecimal;

  36 import jdk.internal.vm.annotation.IntrinsicCandidate;
  37 
  38 /**
  39  * The {@code Float} class is the {@linkplain
  40  * java.lang##wrapperClass wrapper class} for values of the primitive
  41  * type {@code float}. An object of type {@code Float} contains a
  42  * single field whose type is {@code float}.
  43  *
  44  * <p>In addition, this class provides several methods for converting a
  45  * {@code float} to a {@code String} and a
  46  * {@code String} to a {@code float}, as well as other
  47  * constants and methods useful when dealing with a
  48  * {@code float}.
  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  *
  56  * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
  57  * and Comparison</a></h2>
  58  *
  59  * The class {@code java.lang.Double} has a {@linkplain
  60  * Double##equivalenceRelation discussion of equality,
  61  * equivalence, and comparison of floating-point values} that is
  62  * equally applicable to {@code float} values.
  63  *
  64  * <h2><a id=decimalToBinaryConversion>Decimal &harr; Binary Conversion Issues</a></h2>
  65  *
  66  * The {@linkplain Double##decimalToBinaryConversion discussion of binary to
  67  * decimal conversion issues} in {@code java.lang.Double} is also
  68  * applicable to {@code float} values.
  69  *
  70  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
  71  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
  72  *
  73  * @author  Lee Boynton
  74  * @author  Arthur van Hoff
  75  * @author  Joseph D. Darcy
  76  * @since 1.0
  77  */

  78 @jdk.internal.ValueBased
  79 public final class Float extends Number
  80         implements Comparable<Float>, Constable, ConstantDesc {
  81     /**
  82      * A constant holding the positive infinity of type
  83      * {@code float}. It is equal to the value returned by
  84      * {@code Float.intBitsToFloat(0x7f800000)}.
  85      */
  86     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  87 
  88     /**
  89      * A constant holding the negative infinity of type
  90      * {@code float}. It is equal to the value returned by
  91      * {@code Float.intBitsToFloat(0xff800000)}.
  92      */
  93     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  94 
  95     /**
  96      * A constant holding a Not-a-Number (NaN) value of type
  97      * {@code float}.  It is equivalent to the value returned by

 557      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 558      */
 559     public static Float valueOf(String s) throws NumberFormatException {
 560         return new Float(parseFloat(s));
 561     }
 562 
 563     /**
 564      * Returns a {@code Float} instance representing the specified
 565      * {@code float} value.
 566      * If a new {@code Float} instance is not required, this method
 567      * should generally be used in preference to the constructor
 568      * {@link #Float(float)}, as this method is likely to yield
 569      * significantly better space and time performance by caching
 570      * frequently requested values.
 571      *
 572      * @param  f a float value.
 573      * @return a {@code Float} instance representing {@code f}.
 574      * @since  1.5
 575      */
 576     @IntrinsicCandidate

 577     public static Float valueOf(float f) {
 578         return new Float(f);
 579     }
 580 
 581     /**
 582      * Returns a new {@code float} initialized to the value
 583      * represented by the specified {@code String}, as performed
 584      * by the {@code valueOf} method of class {@code Float}.
 585      *
 586      * @param  s the string to be parsed.
 587      * @return the {@code float} value represented by the string
 588      *         argument.
 589      * @throws NullPointerException  if the string is null
 590      * @throws NumberFormatException if the string does not contain a
 591      *               parsable {@code float}.
 592      * @see    java.lang.Float#valueOf(String)
 593      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 594      * @since 1.2
 595      */
 596     public static float parseFloat(String s) throws NumberFormatException {

  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.FloatConsts;
  34 import jdk.internal.math.FloatingDecimal;
  35 import jdk.internal.math.FloatToDecimal;
  36 import jdk.internal.value.DeserializeConstructor;
  37 import jdk.internal.vm.annotation.IntrinsicCandidate;
  38 
  39 /**
  40  * The {@code Float} class is the {@linkplain
  41  * java.lang##wrapperClass wrapper class} for values of the primitive
  42  * type {@code float}. An object of type {@code Float} contains a
  43  * single field whose type is {@code float}.
  44  *
  45  * <p>In addition, this class provides several methods for converting a
  46  * {@code float} to a {@code String} and a
  47  * {@code String} to a {@code float}, as well as other
  48  * constants and methods useful when dealing with a
  49  * {@code float}.
  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 {@linkplain #equals(Object) equal}
  53  * as interchangeable and should not use instances for synchronization, mutexes, or
  54  * with {@linkplain java.lang.ref.Reference object references}.
  55  *
  56  * <div class="preview-block">
  57  *      <div class="preview-comment">
  58  *          When preview features are enabled, {@code Float} is a {@linkplain Class#isValue value class}.
  59  *          Use of value class instances for synchronization, mutexes, or with
  60  *          {@linkplain java.lang.ref.Reference object references} result in
  61  *          {@link IdentityException}.
  62  *      </div>
  63  * </div>
  64  *
  65  * <h2><a id=equivalenceRelation>Floating-point Equality, Equivalence,
  66  * and Comparison</a></h2>
  67  *
  68  * The class {@code java.lang.Double} has a {@linkplain
  69  * Double##equivalenceRelation discussion of equality,
  70  * equivalence, and comparison of floating-point values} that is
  71  * equally applicable to {@code float} values.
  72  *
  73  * <h2><a id=decimalToBinaryConversion>Decimal &harr; Binary Conversion Issues</a></h2>
  74  *
  75  * The {@linkplain Double##decimalToBinaryConversion discussion of binary to
  76  * decimal conversion issues} in {@code java.lang.Double} is also
  77  * applicable to {@code float} values.
  78  *
  79  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
  80  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
  81  *
  82  * @author  Lee Boynton
  83  * @author  Arthur van Hoff
  84  * @author  Joseph D. Darcy
  85  * @since 1.0
  86  */
  87 @jdk.internal.MigratedValueClass
  88 @jdk.internal.ValueBased
  89 public final class Float extends Number
  90         implements Comparable<Float>, Constable, ConstantDesc {
  91     /**
  92      * A constant holding the positive infinity of type
  93      * {@code float}. It is equal to the value returned by
  94      * {@code Float.intBitsToFloat(0x7f800000)}.
  95      */
  96     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  97 
  98     /**
  99      * A constant holding the negative infinity of type
 100      * {@code float}. It is equal to the value returned by
 101      * {@code Float.intBitsToFloat(0xff800000)}.
 102      */
 103     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
 104 
 105     /**
 106      * A constant holding a Not-a-Number (NaN) value of type
 107      * {@code float}.  It is equivalent to the value returned by

 567      * @see Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 568      */
 569     public static Float valueOf(String s) throws NumberFormatException {
 570         return new Float(parseFloat(s));
 571     }
 572 
 573     /**
 574      * Returns a {@code Float} instance representing the specified
 575      * {@code float} value.
 576      * If a new {@code Float} instance is not required, this method
 577      * should generally be used in preference to the constructor
 578      * {@link #Float(float)}, as this method is likely to yield
 579      * significantly better space and time performance by caching
 580      * frequently requested values.
 581      *
 582      * @param  f a float value.
 583      * @return a {@code Float} instance representing {@code f}.
 584      * @since  1.5
 585      */
 586     @IntrinsicCandidate
 587     @DeserializeConstructor
 588     public static Float valueOf(float f) {
 589         return new Float(f);
 590     }
 591 
 592     /**
 593      * Returns a new {@code float} initialized to the value
 594      * represented by the specified {@code String}, as performed
 595      * by the {@code valueOf} method of class {@code Float}.
 596      *
 597      * @param  s the string to be parsed.
 598      * @return the {@code float} value represented by the string
 599      *         argument.
 600      * @throws NullPointerException  if the string is null
 601      * @throws NumberFormatException if the string does not contain a
 602      *               parsable {@code float}.
 603      * @see    java.lang.Float#valueOf(String)
 604      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 605      * @since 1.2
 606      */
 607     public static float parseFloat(String s) throws NumberFormatException {
< prev index next >