< prev index next >

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

Print this page

  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

  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

  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
  61  * {@linkplain #equals(Object) equal} as interchangeable and should not
  62  * use instances for synchronization, or unpredictable behavior may
  63  * occur. For example, in a future release, synchronization may fail.
  64  *
  65  * <p>Implementation note: The implementations of the "bit twiddling"
  66  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  67  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  68  * based on material from Henry S. Warren, Jr.'s <cite>Hacker's
  69  * Delight</cite>, (Addison Wesley, 2002) and <cite>Hacker's
  70  * Delight, Second Edition</cite>, (Pearson Education, 2013).
  71  *
  72  * @author  Lee Boynton
  73  * @author  Arthur van Hoff
  74  * @author  Josh Bloch
  75  * @author  Joseph D. Darcy
  76  * @since 1.0
  77  */
  78 @jdk.internal.MigratedValueClass
  79 @jdk.internal.ValueBased
  80 public final class Integer extends Number
  81         implements Comparable<Integer>, Constable, ConstantDesc {
  82     /**
  83      * A constant holding the minimum value an {@code int} can
  84      * have, -2<sup>31</sup>.
  85      */
  86     @Native public static final int   MIN_VALUE = 0x80000000;
  87 
  88     /**
  89      * A constant holding the maximum value an {@code int} can
  90      * have, 2<sup>31</sup>-1.
  91      */
  92     @Native public static final int   MAX_VALUE = 0x7fffffff;
  93 
  94     /**
  95      * The {@code Class} instance representing the primitive type
  96      * {@code int}.
  97      *
  98      * @since   1.1

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