9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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 * @since 1.0
72 */
73 @jdk.internal.ValueBased
74 public final class Integer extends Number
75 implements Comparable<Integer>, Constable, ConstantDesc {
76 /**
77 * A constant holding the minimum value an {@code int} can
78 * have, -2<sup>31</sup>.
79 */
80 @Native public static final int MIN_VALUE = 0x80000000;
81
82 /**
83 * A constant holding the maximum value an {@code int} can
84 * have, 2<sup>31</sup>-1.
85 */
86 @Native public static final int MAX_VALUE = 0x7fffffff;
87
88 /**
89 * The {@code Class} instance representing the primitive type
90 * {@code int}.
91 *
92 * @since 1.1
933 c[i] = archivedCache[i];
934 assert j == archivedCache[i];
935 j++;
936 }
937 // Fill the rest of the cache.
938 for (int i = archivedSize; i < size; i++) {
939 c[i] = new Integer(j++);
940 }
941 archivedCache = c;
942 }
943 cache = archivedCache;
944 // range [-128, 127] must be interned (JLS7 5.1.7)
945 assert IntegerCache.high >= 127;
946 }
947
948 private IntegerCache() {}
949 }
950
951 /**
952 * Returns an {@code Integer} instance representing the specified
953 * {@code int} value. If a new {@code Integer} instance is not
954 * required, this method should generally be used in preference to
955 * the constructor {@link #Integer(int)}, as this method is likely
956 * to yield significantly better space and time performance by
957 * caching frequently requested values.
958 *
959 * This method will always cache values in the range -128 to 127,
960 * inclusive, and may cache other values outside of this range.
961 *
962 * @param i an {@code int} value.
963 * @return an {@code Integer} instance representing {@code i}.
964 * @since 1.5
965 */
966 @IntrinsicCandidate
967 public static Integer valueOf(int i) {
968 if (i >= IntegerCache.low && i <= IntegerCache.high)
969 return IntegerCache.cache[i + (-IntegerCache.low)];
970 return new Integer(i);
971 }
972
973 /**
974 * The value of the {@code Integer}.
975 *
976 * @serial
977 */
978 private final int value;
979
980 /**
981 * Constructs a newly allocated {@code Integer} object that
982 * represents the specified {@code int} value.
983 *
984 * @param value the value to be represented by the
985 * {@code Integer} object.
986 *
987 * @deprecated
988 * It is rarely appropriate to use this constructor. The static factory
989 * {@link #valueOf(int)} is generally a better choice, as it is
|
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
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.PreviewFeatures;
30 import jdk.internal.misc.VM;
31 import jdk.internal.util.DecimalDigits;
32 import jdk.internal.value.DeserializeConstructor;
33 import jdk.internal.vm.annotation.ForceInline;
34 import jdk.internal.vm.annotation.IntrinsicCandidate;
35 import jdk.internal.vm.annotation.Stable;
36
37 import java.lang.annotation.Native;
38 import java.lang.constant.Constable;
39 import java.lang.constant.ConstantDesc;
40 import java.lang.invoke.MethodHandles;
41 import java.util.Objects;
42 import java.util.Optional;
43
44 import static java.lang.Character.digit;
45 import static java.lang.String.COMPACT_STRINGS;
46 import static java.lang.String.LATIN1;
47 import static java.lang.String.UTF16;
48
49 /**
50 * The {@code Integer} class is the {@linkplain
51 * java.lang##wrapperClass wrapper class} for values of the primitive
52 * type {@code int}. An object of type {@code Integer} contains a
53 * single field whose type is {@code int}.
54 *
55 * <p>In addition, this class provides several methods for converting
56 * an {@code int} to a {@code String} and a {@code String} to an
57 * {@code int}, as well as other constants and methods useful when
58 * dealing with an {@code int}.
59 *
60 * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
61 * class; programmers should treat instances that are {@linkplain #equals(Object) equal}
62 * as interchangeable and should not use instances for synchronization, mutexes, or
63 * with {@linkplain java.lang.ref.Reference object references}.
64 *
65 * <div class="preview-block">
66 * <div class="preview-comment">
67 * When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
68 * Use of value class instances for synchronization, mutexes, or with
69 * {@linkplain java.lang.ref.Reference object references} result in
70 * {@link IdentityException}.
71 * </div>
72 * </div>
73 *
74 * <p>Implementation note: The implementations of the "bit twiddling"
75 * methods (such as {@link #highestOneBit(int) highestOneBit} and
76 * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
77 * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
78 * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
79 * Delight, Second Edition</cite>, (Pearson Education, 2013).
80 *
81 * @since 1.0
82 */
83 @jdk.internal.MigratedValueClass
84 @jdk.internal.ValueBased
85 public final class Integer extends Number
86 implements Comparable<Integer>, Constable, ConstantDesc {
87 /**
88 * A constant holding the minimum value an {@code int} can
89 * have, -2<sup>31</sup>.
90 */
91 @Native public static final int MIN_VALUE = 0x80000000;
92
93 /**
94 * A constant holding the maximum value an {@code int} can
95 * have, 2<sup>31</sup>-1.
96 */
97 @Native public static final int MAX_VALUE = 0x7fffffff;
98
99 /**
100 * The {@code Class} instance representing the primitive type
101 * {@code int}.
102 *
103 * @since 1.1
944 c[i] = archivedCache[i];
945 assert j == archivedCache[i];
946 j++;
947 }
948 // Fill the rest of the cache.
949 for (int i = archivedSize; i < size; i++) {
950 c[i] = new Integer(j++);
951 }
952 archivedCache = c;
953 }
954 cache = archivedCache;
955 // range [-128, 127] must be interned (JLS7 5.1.7)
956 assert IntegerCache.high >= 127;
957 }
958
959 private IntegerCache() {}
960 }
961
962 /**
963 * Returns an {@code Integer} instance representing the specified
964 * {@code int} value.
965 * <div class="preview-block">
966 * <div class="preview-comment">
967 * <p>
968 * - When preview features are NOT enabled, {@code Integer} is an identity class.
969 * If a new {@code Integer} instance is not
970 * required, this method should generally be used in preference to
971 * the constructor {@link #Integer(int)}, as this method is likely
972 * to yield significantly better space and time performance by
973 * caching frequently requested values.
974 * This method will always cache values in the range -128 to 127,
975 * inclusive, and may cache other values outside of this range.
976 * </p>
977 * <p>
978 * - When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
979 * The {@code valueOf} behavior is the same as invoking the constructor,
980 * whether cached or not.
981 * </p>
982 * </div>
983 * </div>
984 *
985 * @param i an {@code int} value.
986 * @return an {@code Integer} instance representing {@code i}.
987 * @since 1.5
988 */
989 @IntrinsicCandidate
990 @DeserializeConstructor
991 public static Integer valueOf(int i) {
992 if (!PreviewFeatures.isEnabled()) {
993 if (i >= IntegerCache.low && i <= IntegerCache.high)
994 return IntegerCache.cache[i + (-IntegerCache.low)];
995 }
996 return new Integer(i);
997 }
998
999 /**
1000 * The value of the {@code Integer}.
1001 *
1002 * @serial
1003 */
1004 private final int value;
1005
1006 /**
1007 * Constructs a newly allocated {@code Integer} object that
1008 * represents the specified {@code int} value.
1009 *
1010 * @param value the value to be represented by the
1011 * {@code Integer} object.
1012 *
1013 * @deprecated
1014 * It is rarely appropriate to use this constructor. The static factory
1015 * {@link #valueOf(int)} is generally a better choice, as it is
|