11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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 jdk.internal.misc.CDS;
29 import jdk.internal.misc.VM;
30 import jdk.internal.util.DecimalDigits;
31 import jdk.internal.vm.annotation.ForceInline;
32 import jdk.internal.vm.annotation.IntrinsicCandidate;
33 import jdk.internal.vm.annotation.Stable;
34
35 import java.lang.annotation.Native;
36 import java.lang.constant.Constable;
37 import java.lang.constant.ConstantDesc;
38 import java.lang.invoke.MethodHandles;
39 import java.util.Objects;
40 import java.util.Optional;
41
42 import static java.lang.Character.digit;
43 import static java.lang.String.COMPACT_STRINGS;
44 import static java.lang.String.LATIN1;
45 import static java.lang.String.UTF16;
46
47 /**
48 * The {@code Integer} class is the {@linkplain
49 * java.lang##wrapperClass wrapper class} for values of the primitive
50 * type {@code int}. An object of type {@code Integer} contains a
51 * single field whose type is {@code int}.
52 *
53 * <p>In addition, this class provides several methods for converting
54 * an {@code int} to a {@code String} and a {@code String} to an
55 * {@code int}, as well as other constants and methods useful when
56 * dealing with an {@code int}.
57 *
58 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
59 * class; programmers should treat instances that are
60 * {@linkplain #equals(Object) equal} as interchangeable and should not
61 * use instances for synchronization, or unpredictable behavior may
62 * occur. For example, in a future release, synchronization may fail.
63 *
64 * <p>Implementation note: The implementations of the "bit twiddling"
65 * methods (such as {@link #highestOneBit(int) highestOneBit} and
66 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
67 * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
68 * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
69 * Delight, Second Edition</cite>, (Pearson Education, 2013).
70 *
71 * @author Lee Boynton
72 * @author Arthur van Hoff
73 * @author Josh Bloch
74 * @author Joseph D. Darcy
75 * @since 1.0
76 */
77 @jdk.internal.ValueBased
78 public final class Integer extends Number
79 implements Comparable<Integer>, Constable, ConstantDesc {
80 /**
81 * A constant holding the minimum value an {@code int} can
82 * have, -2<sup>31</sup>.
83 */
84 @Native public static final int MIN_VALUE = 0x80000000;
85
86 /**
87 * A constant holding the maximum value an {@code int} can
88 * have, 2<sup>31</sup>-1.
89 */
90 @Native public static final int MAX_VALUE = 0x7fffffff;
91
92 /**
93 * The {@code Class} instance representing the primitive type
94 * {@code int}.
95 *
96 * @since 1.1
982
983 private IntegerCache() {}
984 }
985
986 /**
987 * Returns an {@code Integer} instance representing the specified
988 * {@code int} value. If a new {@code Integer} instance is not
989 * required, this method should generally be used in preference to
990 * the constructor {@link #Integer(int)}, as this method is likely
991 * to yield significantly better space and time performance by
992 * caching frequently requested values.
993 *
994 * This method will always cache values in the range -128 to 127,
995 * inclusive, and may cache other values outside of this range.
996 *
997 * @param i an {@code int} value.
998 * @return an {@code Integer} instance representing {@code i}.
999 * @since 1.5
1000 */
1001 @IntrinsicCandidate
1002 public static Integer valueOf(int i) {
1003 if (i >= IntegerCache.low && i <= IntegerCache.high)
1004 return IntegerCache.cache[i + (-IntegerCache.low)];
1005 return new Integer(i);
1006 }
1007
1008 /**
1009 * The value of the {@code Integer}.
1010 *
1011 * @serial
1012 */
1013 private final int value;
1014
1015 /**
1016 * Constructs a newly allocated {@code Integer} object that
1017 * represents the specified {@code int} value.
1018 *
1019 * @param value the value to be represented by the
1020 * {@code Integer} object.
1021 *
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
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 jdk.internal.misc.CDS;
29 import jdk.internal.misc.VM;
30 import jdk.internal.util.DecimalDigits;
31 import jdk.internal.value.DeserializeConstructor;
32 import jdk.internal.vm.annotation.ForceInline;
33 import jdk.internal.vm.annotation.IntrinsicCandidate;
34 import jdk.internal.vm.annotation.Stable;
35
36 import java.lang.annotation.Native;
37 import java.lang.constant.Constable;
38 import java.lang.constant.ConstantDesc;
39 import java.lang.invoke.MethodHandles;
40 import java.util.Objects;
41 import java.util.Optional;
42
43 import static java.lang.Character.digit;
44 import static java.lang.String.COMPACT_STRINGS;
45 import static java.lang.String.LATIN1;
46 import static java.lang.String.UTF16;
47
48 /**
49 * The {@code Integer} class is the {@linkplain
50 * java.lang##wrapperClass wrapper class} for values of the primitive
51 * type {@code int}. An object of type {@code Integer} contains a
52 * single field whose type is {@code int}.
53 *
54 * <p>In addition, this class provides several methods for converting
55 * an {@code int} to a {@code String} and a {@code String} to an
56 * {@code int}, as well as other constants and methods useful when
57 * dealing with an {@code int}.
58 *
59 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
60 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
61 * as interchangeable and should not use instances for synchronization, mutexes, or
62 * with {@linkplain java.lang.ref.Reference object references}.
63 *
64 * <div class="preview-block">
65 * <div class="preview-comment">
66 * When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
67 * Use of value class instances for synchronization, mutexes, or with
68 * {@linkplain java.lang.ref.Reference object references} result in
69 * {@link IdentityException}.
70 * </div>
71 * </div>
72 *
73 * <p>Implementation note: The implementations of the "bit twiddling"
74 * methods (such as {@link #highestOneBit(int) highestOneBit} and
75 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
76 * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
77 * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
78 * Delight, Second Edition</cite>, (Pearson Education, 2013).
79 *
80 * @author Lee Boynton
81 * @author Arthur van Hoff
82 * @author Josh Bloch
83 * @author Joseph D. Darcy
84 * @since 1.0
85 */
86 @jdk.internal.MigratedValueClass
87 @jdk.internal.ValueBased
88 public final class Integer extends Number
89 implements Comparable<Integer>, Constable, ConstantDesc {
90 /**
91 * A constant holding the minimum value an {@code int} can
92 * have, -2<sup>31</sup>.
93 */
94 @Native public static final int MIN_VALUE = 0x80000000;
95
96 /**
97 * A constant holding the maximum value an {@code int} can
98 * have, 2<sup>31</sup>-1.
99 */
100 @Native public static final int MAX_VALUE = 0x7fffffff;
101
102 /**
103 * The {@code Class} instance representing the primitive type
104 * {@code int}.
105 *
106 * @since 1.1
992
993 private IntegerCache() {}
994 }
995
996 /**
997 * Returns an {@code Integer} instance representing the specified
998 * {@code int} value. If a new {@code Integer} instance is not
999 * required, this method should generally be used in preference to
1000 * the constructor {@link #Integer(int)}, as this method is likely
1001 * to yield significantly better space and time performance by
1002 * caching frequently requested values.
1003 *
1004 * This method will always cache values in the range -128 to 127,
1005 * inclusive, and may cache other values outside of this range.
1006 *
1007 * @param i an {@code int} value.
1008 * @return an {@code Integer} instance representing {@code i}.
1009 * @since 1.5
1010 */
1011 @IntrinsicCandidate
1012 @DeserializeConstructor
1013 public static Integer valueOf(int i) {
1014 if (i >= IntegerCache.low && i <= IntegerCache.high)
1015 return IntegerCache.cache[i + (-IntegerCache.low)];
1016 return new Integer(i);
1017 }
1018
1019 /**
1020 * The value of the {@code Integer}.
1021 *
1022 * @serial
1023 */
1024 private final int value;
1025
1026 /**
1027 * Constructs a newly allocated {@code Integer} object that
1028 * represents the specified {@code int} value.
1029 *
1030 * @param value the value to be represented by the
1031 * {@code Integer} object.
1032 *
|