< 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 wraps a value of primitive type
  40  * {@code float} in an object. An object of type
  41  * {@code Float} contains a single field whose type is
  42  * {@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  *

  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

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

 578     public static Float valueOf(float f) {
 579         return new Float(f);
 580     }
 581 
 582     /**
 583      * Returns a new {@code float} initialized to the value
 584      * represented by the specified {@code String}, as performed
 585      * by the {@code valueOf} method of class {@code Float}.
 586      *
 587      * @param  s the string to be parsed.
 588      * @return the {@code float} value represented by the string
 589      *         argument.
 590      * @throws NullPointerException  if the string is null
 591      * @throws NumberFormatException if the string does not contain a
 592      *               parsable {@code float}.
 593      * @see    java.lang.Float#valueOf(String)
 594      * @see    Double##decimalToBinaryConversion Decimal &harr; Binary Conversion Issues
 595      * @since 1.2
 596      */
 597     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 wraps a value of primitive type
  41  * {@code float} in an object. An object of type
  42  * {@code Float} contains a single field whose type is
  43  * {@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
  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  *

  59  *
  60  * The class {@code java.lang.Double} has a {@linkplain
  61  * Double##equivalenceRelation discussion of equality,
  62  * equivalence, and comparison of floating-point values} that is
  63  * equally applicable to {@code float} values.
  64  *
  65  * <h2><a id=decimalToBinaryConversion>Decimal &harr; Binary Conversion Issues</a></h2>
  66  *
  67  * The {@linkplain Double##decimalToBinaryConversion discussion of binary to
  68  * decimal conversion issues} in {@code java.lang.Double} is also
  69  * applicable to {@code float} values.
  70  *
  71  * @see <a href="https://standards.ieee.org/ieee/754/6210/">
  72  *      <cite>IEEE Standard for Floating-Point Arithmetic</cite></a>
  73  *
  74  * @author  Lee Boynton
  75  * @author  Arthur van Hoff
  76  * @author  Joseph D. Darcy
  77  * @since 1.0
  78  */
  79 @jdk.internal.MigratedValueClass
  80 @jdk.internal.ValueBased
  81 public final class Float extends Number
  82         implements Comparable<Float>, Constable, ConstantDesc {
  83     /**
  84      * A constant holding the positive infinity of type
  85      * {@code float}. It is equal to the value returned by
  86      * {@code Float.intBitsToFloat(0x7f800000)}.
  87      */
  88     public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
  89 
  90     /**
  91      * A constant holding the negative infinity of type
  92      * {@code float}. It is equal to the value returned by
  93      * {@code Float.intBitsToFloat(0xff800000)}.
  94      */
  95     public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
  96 
  97     /**
  98      * A constant holding a Not-a-Number (NaN) value of type
  99      * {@code float}.  It is equivalent to the value returned by

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