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 ↔ 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 * @spec https://standards.ieee.org/ieee/754/6210/
71 * IEEE Standard for Floating-Point Arithmetic
72 *
73 * @since 1.0
74 */
75 @jdk.internal.ValueBased
76 public final class Float extends Number
77 implements Comparable<Float>, Constable, ConstantDesc {
78 /**
79 * A constant holding the positive infinity of type
80 * {@code float}. It is equal to the value returned by
81 * {@code Float.intBitsToFloat(0x7f800000)}.
82 */
83 public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
84
85 /**
86 * A constant holding the negative infinity of type
87 * {@code float}. It is equal to the value returned by
88 * {@code Float.intBitsToFloat(0xff800000)}.
89 */
90 public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
91
92 /**
93 * A constant holding a Not-a-Number (NaN) value of type {@code float}.
94 * It is {@linkplain Double##equivalenceRelation equivalent}
555 * @see Double##decimalToBinaryConversion Decimal ↔ Binary Conversion Issues
556 */
557 public static Float valueOf(String s) throws NumberFormatException {
558 return new Float(parseFloat(s));
559 }
560
561 /**
562 * Returns a {@code Float} instance representing the specified
563 * {@code float} value.
564 * If a new {@code Float} instance is not required, this method
565 * should generally be used in preference to the constructor
566 * {@link #Float(float)}, as this method is likely to yield
567 * significantly better space and time performance by caching
568 * frequently requested values.
569 *
570 * @param f a float value.
571 * @return a {@code Float} instance representing {@code f}.
572 * @since 1.5
573 */
574 @IntrinsicCandidate
575 public static Float valueOf(float f) {
576 return new Float(f);
577 }
578
579 /**
580 * Returns a new {@code float} initialized to the value
581 * represented by the specified {@code String}, as performed
582 * by the {@code valueOf} method of class {@code Float}.
583 *
584 * @param s the string to be parsed.
585 * @return the {@code float} value represented by the string
586 * argument.
587 * @throws NullPointerException if the string is null
588 * @throws NumberFormatException if the string does not contain a
589 * parsable {@code float}.
590 * @see java.lang.Float#valueOf(String)
591 * @see Double##decimalToBinaryConversion Decimal ↔ Binary Conversion Issues
592 * @since 1.2
593 */
594 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 ↔ 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 * @spec https://standards.ieee.org/ieee/754/6210/
80 * IEEE Standard for Floating-Point Arithmetic
81 *
82 * @since 1.0
83 */
84 @jdk.internal.MigratedValueClass
85 @jdk.internal.ValueBased
86 public final class Float extends Number
87 implements Comparable<Float>, Constable, ConstantDesc {
88 /**
89 * A constant holding the positive infinity of type
90 * {@code float}. It is equal to the value returned by
91 * {@code Float.intBitsToFloat(0x7f800000)}.
92 */
93 public static final float POSITIVE_INFINITY = 1.0f / 0.0f;
94
95 /**
96 * A constant holding the negative infinity of type
97 * {@code float}. It is equal to the value returned by
98 * {@code Float.intBitsToFloat(0xff800000)}.
99 */
100 public static final float NEGATIVE_INFINITY = -1.0f / 0.0f;
101
102 /**
103 * A constant holding a Not-a-Number (NaN) value of type {@code float}.
104 * It is {@linkplain Double##equivalenceRelation equivalent}
565 * @see Double##decimalToBinaryConversion Decimal ↔ Binary Conversion Issues
566 */
567 public static Float valueOf(String s) throws NumberFormatException {
568 return new Float(parseFloat(s));
569 }
570
571 /**
572 * Returns a {@code Float} instance representing the specified
573 * {@code float} value.
574 * If a new {@code Float} instance is not required, this method
575 * should generally be used in preference to the constructor
576 * {@link #Float(float)}, as this method is likely to yield
577 * significantly better space and time performance by caching
578 * frequently requested values.
579 *
580 * @param f a float value.
581 * @return a {@code Float} instance representing {@code f}.
582 * @since 1.5
583 */
584 @IntrinsicCandidate
585 @DeserializeConstructor
586 public static Float valueOf(float f) {
587 return new Float(f);
588 }
589
590 /**
591 * Returns a new {@code float} initialized to the value
592 * represented by the specified {@code String}, as performed
593 * by the {@code valueOf} method of class {@code Float}.
594 *
595 * @param s the string to be parsed.
596 * @return the {@code float} value represented by the string
597 * argument.
598 * @throws NullPointerException if the string is null
599 * @throws NumberFormatException if the string does not contain a
600 * parsable {@code float}.
601 * @see java.lang.Float#valueOf(String)
602 * @see Double##decimalToBinaryConversion Decimal ↔ Binary Conversion Issues
603 * @since 1.2
604 */
605 public static float parseFloat(String s) throws NumberFormatException {
|