< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page

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

 969                     c[i] = archivedCache[i];
 970                     assert j == archivedCache[i];
 971                     j++;
 972                 }
 973                 // Fill the rest of the cache.
 974                 for (int i = archivedSize; i < size; i++) {
 975                     c[i] = new Integer(j++);
 976                 }
 977                 archivedCache = c;
 978             }
 979             cache = archivedCache;
 980             // range [-128, 127] must be interned (JLS7 5.1.7)
 981             assert IntegerCache.high >= 127;
 982         }
 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      *
1023      * @deprecated
1024      * It is rarely appropriate to use this constructor. The static factory
1025      * {@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  * @author  Lee Boynton
  82  * @author  Arthur van Hoff
  83  * @author  Josh Bloch
  84  * @author  Joseph D. Darcy
  85  * @since 1.0
  86  */
  87 @jdk.internal.MigratedValueClass
  88 @jdk.internal.ValueBased
  89 public final class Integer extends Number
  90         implements Comparable<Integer>, Constable, ConstantDesc {
  91     /**
  92      * A constant holding the minimum value an {@code int} can
  93      * have, -2<sup>31</sup>.
  94      */
  95     @Native public static final int   MIN_VALUE = 0x80000000;
  96 
  97     /**
  98      * A constant holding the maximum value an {@code int} can
  99      * have, 2<sup>31</sup>-1.
 100      */
 101     @Native public static final int   MAX_VALUE = 0x7fffffff;
 102 
 103     /**
 104      * The {@code Class} instance representing the primitive type
 105      * {@code int}.
 106      *
 107      * @since   1.1

 980                     c[i] = archivedCache[i];
 981                     assert j == archivedCache[i];
 982                     j++;
 983                 }
 984                 // Fill the rest of the cache.
 985                 for (int i = archivedSize; i < size; i++) {
 986                     c[i] = new Integer(j++);
 987                 }
 988                 archivedCache = c;
 989             }
 990             cache = archivedCache;
 991             // range [-128, 127] must be interned (JLS7 5.1.7)
 992             assert IntegerCache.high >= 127;
 993         }
 994 
 995         private IntegerCache() {}
 996     }
 997 
 998     /**
 999      * Returns an {@code Integer} instance representing the specified
1000      * {@code int} value.
1001      * <div class="preview-block">
1002      *      <div class="preview-comment">
1003      *          <p>
1004      *              - When preview features are NOT enabled, {@code Integer} is an identity class.
1005      *              If a new {@code Integer} instance is not
1006      *              required, this method should generally be used in preference to
1007      *              the constructor {@link #Integer(int)}, as this method is likely
1008      *              to yield significantly better space and time performance by
1009      *              caching frequently requested values.
1010      *              This method will always cache values in the range -128 to 127,
1011      *              inclusive, and may cache other values outside of this range.
1012      *          </p>
1013      *          <p>
1014      *              - When preview features are enabled, {@code Integer} is a {@linkplain Class#isValue value class}.
1015      *              The {@code valueOf} behavior is the same as invoking the constructor,
1016      *              whether cached or not.
1017      *          </p>
1018      *      </div>
1019      * </div>
1020      *
1021      * @param  i an {@code int} value.
1022      * @return an {@code Integer} instance representing {@code i}.
1023      * @since  1.5
1024      */
1025     @IntrinsicCandidate
1026     @DeserializeConstructor
1027     public static Integer valueOf(int i) {
1028         if (!PreviewFeatures.isEnabled()) {
1029             if (i >= IntegerCache.low && i <= IntegerCache.high)
1030                 return IntegerCache.cache[i + (-IntegerCache.low)];
1031         }
1032         return new Integer(i);
1033     }
1034 
1035     /**
1036      * The value of the {@code Integer}.
1037      *
1038      * @serial
1039      */
1040     private final int value;
1041 
1042     /**
1043      * Constructs a newly allocated {@code Integer} object that
1044      * represents the specified {@code int} value.
1045      *
1046      * @param   value   the value to be represented by the
1047      *                  {@code Integer} object.
1048      *
1049      * @deprecated
1050      * It is rarely appropriate to use this constructor. The static factory
1051      * {@link #valueOf(int)} is generally a better choice, as it is
< prev index next >