< 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 wraps a value of the primitive type
  49  * {@code int} in an object. An object of type {@code Integer}
  50  * contains a single field whose type is {@code int}.

  55  * dealing with an {@code int}.
  56  *
  57  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
  58  * class; programmers should treat instances that are
  59  * {@linkplain #equals(Object) equal} as interchangeable and should not
  60  * use instances for synchronization, or unpredictable behavior may
  61  * occur. For example, in a future release, synchronization may fail.
  62  *
  63  * <p>Implementation note: The implementations of the "bit twiddling"
  64  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  65  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  66  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  67  * Delight</i>, (Addison Wesley, 2002).
  68  *
  69  * @author  Lee Boynton
  70  * @author  Arthur van Hoff
  71  * @author  Josh Bloch
  72  * @author  Joseph D. Darcy
  73  * @since 1.0
  74  */

  75 @jdk.internal.ValueBased
  76 public final class Integer extends Number
  77         implements Comparable<Integer>, Constable, ConstantDesc {
  78     /**
  79      * A constant holding the minimum value an {@code int} can
  80      * have, -2<sup>31</sup>.
  81      */
  82     @Native public static final int   MIN_VALUE = 0x80000000;
  83 
  84     /**
  85      * A constant holding the maximum value an {@code int} can
  86      * have, 2<sup>31</sup>-1.
  87      */
  88     @Native public static final int   MAX_VALUE = 0x7fffffff;
  89 
  90     /**
  91      * The {@code Class} instance representing the primitive type
  92      * {@code int}.
  93      *
  94      * @since   1.1

 971 
 972         private IntegerCache() {}
 973     }
 974 
 975     /**
 976      * Returns an {@code Integer} instance representing the specified
 977      * {@code int} value.  If a new {@code Integer} instance is not
 978      * required, this method should generally be used in preference to
 979      * the constructor {@link #Integer(int)}, as this method is likely
 980      * to yield significantly better space and time performance by
 981      * caching frequently requested values.
 982      *
 983      * This method will always cache values in the range -128 to 127,
 984      * inclusive, and may cache other values outside of this range.
 985      *
 986      * @param  i an {@code int} value.
 987      * @return an {@code Integer} instance representing {@code i}.
 988      * @since  1.5
 989      */
 990     @IntrinsicCandidate

 991     public static Integer valueOf(int i) {
 992         if (i >= IntegerCache.low && i <= IntegerCache.high)
 993             return IntegerCache.cache[i + (-IntegerCache.low)];
 994         return new Integer(i);
 995     }
 996 
 997     /**
 998      * The value of the {@code Integer}.
 999      *
1000      * @serial
1001      */
1002     private final int value;
1003 
1004     /**
1005      * Constructs a newly allocated {@code Integer} object that
1006      * represents the specified {@code int} value.
1007      *
1008      * @param   value   the value to be represented by the
1009      *                  {@code Integer} object.
1010      *

  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 wraps a value of the primitive type
  50  * {@code int} in an object. An object of type {@code Integer}
  51  * contains a single field whose type is {@code int}.

  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 <i>Hacker's
  68  * Delight</i>, (Addison Wesley, 2002).
  69  *
  70  * @author  Lee Boynton
  71  * @author  Arthur van Hoff
  72  * @author  Josh Bloch
  73  * @author  Joseph D. Darcy
  74  * @since 1.0
  75  */
  76 @jdk.internal.MigratedValueClass
  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

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