< 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  *

  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
  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

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