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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   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 java.util.Properties;
  29 
  30 /**
  31  * The {@code Integer} class wraps a value of the primitive type
  32  * {@code int} in an object. An object of type {@code Integer}
  33  * contains a single field whose type is {@code int}.
  34  *
  35  * <p>In addition, this class provides several methods for converting
  36  * an {@code int} to a {@code String} and a {@code String} to an
  37  * {@code int}, as well as other constants and methods useful when
  38  * dealing with an {@code int}.
  39  *
  40  * <p>Implementation note: The implementations of the "bit twiddling"
  41  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  42  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  43  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  44  * Delight</i>, (Addison Wesley, 2002).
  45  *
  46  * @author  Lee Boynton
  47  * @author  Arthur van Hoff
  48  * @author  Josh Bloch
  49  * @author  Joseph D. Darcy
  50  * @since JDK1.0
  51  */
  52 public final class Integer extends Number implements Comparable<Integer> {
  53     /**
  54      * A constant holding the minimum value an {@code int} can
  55      * have, -2<sup>31</sup>.
  56      */
  57     public static final int   MIN_VALUE = 0x80000000;
  58 
  59     /**
  60      * A constant holding the maximum value an {@code int} can
  61      * have, 2<sup>31</sup>-1.
  62      */
  63     public static final int   MAX_VALUE = 0x7fffffff;
  64 
  65     /**
  66      * The {@code Class} instance representing the primitive type
  67      * {@code int}.
  68      *
  69      * @since   JDK1.1
  70      */
  71     @SuppressWarnings("unchecked")
  72     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  73 
  74     /**
  75      * All possible chars for representing a number as a String
  76      */
  77     final static char[] digits = {
  78         '0' , '1' , '2' , '3' , '4' , '5' ,
  79         '6' , '7' , '8' , '9' , 'a' , 'b' ,
  80         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  81         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  82         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  83         'u' , 'v' , 'w' , 'x' , 'y' , 'z'


1278      * @param divisor the value doing the dividing
1279      * @return the unsigned remainder of the first argument divided by
1280      * the second argument
1281      * @see #divideUnsigned
1282      * @since 1.8
1283      */
1284     public static int remainderUnsigned(int dividend, int divisor) {
1285         // In lieu of tricky code, for now just use long arithmetic.
1286         return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
1287     }
1288 
1289 
1290     // Bit twiddling
1291 
1292     /**
1293      * The number of bits used to represent an {@code int} value in two's
1294      * complement binary form.
1295      *
1296      * @since 1.5
1297      */
1298     public static final int SIZE = 32;
1299 
1300     /**
1301      * The number of bytes used to represent a {@code int} value in two's
1302      * complement binary form.
1303      *
1304      * @since 1.8
1305      */
1306     public static final int BYTES = SIZE / Byte.SIZE;
1307 
1308     /**
1309      * Returns an {@code int} value with at most a single one-bit, in the
1310      * position of the highest-order ("leftmost") one-bit in the specified
1311      * {@code int} value.  Returns zero if the specified value has no
1312      * one-bits in its two's complement binary representation, that is, if it
1313      * is equal to zero.
1314      *
1315      * @return an {@code int} value with a single one-bit, in the position
1316      *     of the highest-order one-bit in the specified value, or zero if
1317      *     the specified value is itself equal to zero.
1318      * @since 1.5


1496         // HD, Section 2-7
1497         return (i >> 31) | (-i >>> 31);
1498     }
1499 
1500     /**
1501      * Returns the value obtained by reversing the order of the bytes in the
1502      * two's complement representation of the specified {@code int} value.
1503      *
1504      * @return the value obtained by reversing the bytes in the specified
1505      *     {@code int} value.
1506      * @since 1.5
1507      */
1508     public static int reverseBytes(int i) {
1509         return ((i >>> 24)           ) |
1510                ((i >>   8) &   0xFF00) |
1511                ((i <<   8) & 0xFF0000) |
1512                ((i << 24));
1513     }
1514 
1515     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1516     private static final long serialVersionUID = 1360826667806852920L;
1517 }


   8  * particular file as subject to the "Classpath" exception as provided
   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 java.lang.annotation.Native;
  29 import java.util.Properties;
  30 
  31 /**
  32  * The {@code Integer} class wraps a value of the primitive type
  33  * {@code int} in an object. An object of type {@code Integer}
  34  * contains a single field whose type is {@code int}.
  35  *
  36  * <p>In addition, this class provides several methods for converting
  37  * an {@code int} to a {@code String} and a {@code String} to an
  38  * {@code int}, as well as other constants and methods useful when
  39  * dealing with an {@code int}.
  40  *
  41  * <p>Implementation note: The implementations of the "bit twiddling"
  42  * methods (such as {@link #highestOneBit(int) highestOneBit} and
  43  * {@link #numberOfTrailingZeros(int) numberOfTrailingZeros}) are
  44  * based on material from Henry S. Warren, Jr.'s <i>Hacker's
  45  * Delight</i>, (Addison Wesley, 2002).
  46  *
  47  * @author  Lee Boynton
  48  * @author  Arthur van Hoff
  49  * @author  Josh Bloch
  50  * @author  Joseph D. Darcy
  51  * @since JDK1.0
  52  */
  53 public final class Integer extends Number implements Comparable<Integer> {
  54     /**
  55      * A constant holding the minimum value an {@code int} can
  56      * have, -2<sup>31</sup>.
  57      */
  58     @Native public static final int   MIN_VALUE = 0x80000000;
  59 
  60     /**
  61      * A constant holding the maximum value an {@code int} can
  62      * have, 2<sup>31</sup>-1.
  63      */
  64     @Native public static final int   MAX_VALUE = 0x7fffffff;
  65 
  66     /**
  67      * The {@code Class} instance representing the primitive type
  68      * {@code int}.
  69      *
  70      * @since   JDK1.1
  71      */
  72     @SuppressWarnings("unchecked")
  73     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  74 
  75     /**
  76      * All possible chars for representing a number as a String
  77      */
  78     final static char[] digits = {
  79         '0' , '1' , '2' , '3' , '4' , '5' ,
  80         '6' , '7' , '8' , '9' , 'a' , 'b' ,
  81         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  82         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  83         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  84         'u' , 'v' , 'w' , 'x' , 'y' , 'z'


1279      * @param divisor the value doing the dividing
1280      * @return the unsigned remainder of the first argument divided by
1281      * the second argument
1282      * @see #divideUnsigned
1283      * @since 1.8
1284      */
1285     public static int remainderUnsigned(int dividend, int divisor) {
1286         // In lieu of tricky code, for now just use long arithmetic.
1287         return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor));
1288     }
1289 
1290 
1291     // Bit twiddling
1292 
1293     /**
1294      * The number of bits used to represent an {@code int} value in two's
1295      * complement binary form.
1296      *
1297      * @since 1.5
1298      */
1299     @Native public static final int SIZE = 32;
1300 
1301     /**
1302      * The number of bytes used to represent a {@code int} value in two's
1303      * complement binary form.
1304      *
1305      * @since 1.8
1306      */
1307     public static final int BYTES = SIZE / Byte.SIZE;
1308 
1309     /**
1310      * Returns an {@code int} value with at most a single one-bit, in the
1311      * position of the highest-order ("leftmost") one-bit in the specified
1312      * {@code int} value.  Returns zero if the specified value has no
1313      * one-bits in its two's complement binary representation, that is, if it
1314      * is equal to zero.
1315      *
1316      * @return an {@code int} value with a single one-bit, in the position
1317      *     of the highest-order one-bit in the specified value, or zero if
1318      *     the specified value is itself equal to zero.
1319      * @since 1.5


1497         // HD, Section 2-7
1498         return (i >> 31) | (-i >>> 31);
1499     }
1500 
1501     /**
1502      * Returns the value obtained by reversing the order of the bytes in the
1503      * two's complement representation of the specified {@code int} value.
1504      *
1505      * @return the value obtained by reversing the bytes in the specified
1506      *     {@code int} value.
1507      * @since 1.5
1508      */
1509     public static int reverseBytes(int i) {
1510         return ((i >>> 24)           ) |
1511                ((i >>   8) &   0xFF00) |
1512                ((i <<   8) & 0xFF0000) |
1513                ((i << 24));
1514     }
1515 
1516     /** use serialVersionUID from JDK 1.0.2 for interoperability */
1517     @Native private static final long serialVersionUID = 1360826667806852920L;
1518 }