< 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

 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     public static Integer valueOf(int i) {
 994         if (i >= IntegerCache.low && i <= IntegerCache.high)
 995             return IntegerCache.cache[i + (-IntegerCache.low)];
 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      *

  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

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