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
983
984 private IntegerCache() {}
985 }
986
987 /**
988 * Returns an {@code Integer} instance representing the specified
989 * {@code int} value. If a new {@code Integer} instance is not
990 * required, this method should generally be used in preference to
991 * the constructor {@link #Integer(int)}, as this method is likely
992 * to yield significantly better space and time performance by
993 * caching frequently requested values.
994 *
995 * This method will always cache values in the range -128 to 127,
996 * inclusive, and may cache other values outside of this range.
997 *
998 * @param i an {@code int} value.
999 * @return an {@code Integer} instance representing {@code i}.
1000 * @since 1.5
1001 */
1002 @IntrinsicCandidate
1003 public static Integer valueOf(int i) {
1004 if (i >= IntegerCache.low && i <= IntegerCache.high)
1005 return IntegerCache.cache[i + (-IntegerCache.low)];
1006 return new Integer(i);
1007 }
1008
1009 /**
1010 * The value of the {@code Integer}.
1011 *
1012 * @serial
1013 */
1014 private final int value;
1015
1016 /**
1017 * Constructs a newly allocated {@code Integer} object that
1018 * represents the specified {@code int} value.
1019 *
1020 * @param value the value to be represented by the
1021 * {@code Integer} object.
1022 *
|
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
993
994 private IntegerCache() {}
995 }
996
997 /**
998 * Returns an {@code Integer} instance representing the specified
999 * {@code int} value. If a new {@code Integer} instance is not
1000 * required, this method should generally be used in preference to
1001 * the constructor {@link #Integer(int)}, as this method is likely
1002 * to yield significantly better space and time performance by
1003 * caching frequently requested values.
1004 *
1005 * This method will always cache values in the range -128 to 127,
1006 * inclusive, and may cache other values outside of this range.
1007 *
1008 * @param i an {@code int} value.
1009 * @return an {@code Integer} instance representing {@code i}.
1010 * @since 1.5
1011 */
1012 @IntrinsicCandidate
1013 @DeserializeConstructor
1014 public static Integer valueOf(int i) {
1015 if (i >= IntegerCache.low && i <= IntegerCache.high)
1016 return IntegerCache.cache[i + (-IntegerCache.low)];
1017 return new Integer(i);
1018 }
1019
1020 /**
1021 * The value of the {@code Integer}.
1022 *
1023 * @serial
1024 */
1025 private final int value;
1026
1027 /**
1028 * Constructs a newly allocated {@code Integer} object that
1029 * represents the specified {@code int} value.
1030 *
1031 * @param value the value to be represented by the
1032 * {@code Integer} object.
1033 *
|