Integer 类继承 Number 类,实现 Comparable 接口。Integer 类被修饰为 final,所以其不可被继承。
# 字段
# MIN_VALUE
最小值,@Native 表示该字段可以从本地代码引用。
| @Native public static final int MIN_VALUE = 0x80000000; |
# MAX_VALUE
最大值。
| @Native public static final int MAX_VALUE = 0x7fffffff; |
# TYPE
类原始类型 int 的类实例。@SuppressWarnings ("unchecked") 表示该字段应该抑制 unchecked 的编译器警告。
| @SuppressWarnings("unchecked") |
| public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); |
# digits
所有可以表示为字符的数字。此变量是默认修饰符 ,只能在包内访问。
| final static char[] digits = { |
| '0' , '1' , '2' , '3' , '4' , '5' , |
| '6' , '7' , '8' , '9' , 'a' , 'b' , |
| 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , |
| 'i' , 'j' , 'k' , 'l' , 'm' , 'n' , |
| 'o' , 'p' , 'q' , 'r' , 's' , 't' , |
| 'u' , 'v' , 'w' , 'x' , 'y' , 'z' |
| }; |
# DigitTens
处理大的数,代表十位。
| final static char [] DigitTens = { |
| '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', |
| '1', '1', '1', '1', '1', '1', '1', '1', '1', '1', |
| '2', '2', '2', '2', '2', '2', '2', '2', '2', '2', |
| '3', '3', '3', '3', '3', '3', '3', '3', '3', '3', |
| '4', '4', '4', '4', '4', '4', '4', '4', '4', '4', |
| '5', '5', '5', '5', '5', '5', '5', '5', '5', '5', |
| '6', '6', '6', '6', '6', '6', '6', '6', '6', '6', |
| '7', '7', '7', '7', '7', '7', '7', '7', '7', '7', |
| '8', '8', '8', '8', '8', '8', '8', '8', '8', '8', |
| '9', '9', '9', '9', '9', '9', '9', '9', '9', '9', |
| } ; |
# DigitOnes
处理大的数,代表个位。
| final static char [] DigitOnes = { |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', |
| } ; |
# sizeTable
为了得到十进制的位数。
| final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, |
| 99999999, 999999999, Integer.MAX_VALUE }; |
# value
Integer 的值,value 被定义为 final,即常量,值不可更改。
# SIZE
用于表示二进制补码二进制形式的 int
值的位数。
| @Native public static final int SIZE = 32; |
# BYTES
用于表示二进制补码二进制形式的 int
值的字节数。
| public static final int BYTES = SIZE / Byte.SIZE; |
# serialVersionUID
Integer 的序列化版本号。
| @Native private static final long serialVersionUID = 1360826667806852920L; |
# 构造方法
# Integer(int value)
| public Integer(int value) { |
| this.value = value; |
| } |
# Integer(String s)
| public Integer(String s) throws NumberFormatException { |
| this.value = parseInt(s, 10); |
| } |
# 方法
# toString(int i, int radix)
i 是要转换为字符串的整数,radix 是在字符串表示中使用的基数,返回基数 radix 表示的 i 的字符串。
| public static String toString(int i, int radix) { |
| |
| if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) |
| radix = 10; |
| |
| |
| if (radix == 10) { |
| return toString(i); |
| } |
| |
| |
| char buf[] = new char[33]; |
| boolean negative =(i < 0); |
| |
| int charPos = 32; |
| |
| |
| if (!negative) { |
| i = -i; |
| } |
| |
| |
| |
| while (i <= -radix) { |
| buf[charPos--] = digits[-(i % radix)]; |
| i = i / radix; |
| } |
| buf[charPos] = digits[-i]; |
| |
| |
| if (negative) { |
| buf[--charPos] = '-'; |
| } |
| |
| |
| return new String(buf, charPos, (33 - charPos)); |
| } |
# toUnsignedString(int i, int radix)
i 是要转换为字符串的整数,radix 是在字符串表示中使用的基数,返回无符号整数 i 以基数 radix 表示的字符串。具体参考 Long
的源码阅读。
| public static String toUnsignedString(int i, int radix) { |
| return Long.toUnsignedString(toUnsignedLong(i), radix); |
| } |
# toHexString(int i)
i 是要转换为字符串的整数,返回无符号整数 i 以基数 16 表示的字符串。
| public static String toHexString(int i) { |
| return toUnsignedString0(i, 4); |
| } |
# toOctalString(int i)
i 是要转换为字符串的整数,返回无符号整数 i 以基数 8 表示的字符串。
| public static String toOctalString(int i) { |
| return toUnsignedString0(i, 3); |
| } |
# toBinaryString(int i)
i 是要转换为字符串的整数,返回无符号整数 i 以基数 2 表示的字符串。
| public static String toBinaryString(int i) { |
| return toUnsignedString0(i, 1); |
| } |
# toUnsignedString0(int val, int shift)
将 val 转换为以 shift 表示的基数的字符串,val 是要转换的整数,shift 是log2x 的值,x 是进制数。
| private static String toUnsignedString0(int val, int shift) { |
| |
| |
| int mag = Integer.SIZE - Integer.numberOfLeadingZeros(val); |
| |
| int chars = Math.max(((mag + (shift - 1)) / shift), 1); |
| char[] buf = new char[chars]; |
| |
| |
| formatUnsignedInt(val, shift, buf, 0, chars); |
| |
| |
| return new String(buf, true); |
| } |
val 是要格式化的整数,shift 是要格式化的基数的代表,4 代表十六进制,3 代表八进制,1 代表二进制,buf 是要写入的字符数组,offset 是 buf 中开始的位置,len 是要写入 buf 的长度,返回 buf 中最低位的位置。
| static int formatUnsignedInt(int val, int shift, char[] buf, int offset, int len) { |
| int charPos = len; |
| int radix = 1 << shift; |
| int mask = radix - 1; |
| |
| |
| do { |
| buf[offset + --charPos] = Integer.digits[val & mask]; |
| val >>>= shift; |
| } while (val != 0 && charPos > 0); |
| |
| return charPos; |
| } |
# toString(int i)
将 i 转换为 string 类型,返回 String 类型的 i。
| public static String toString(int i) { |
| if (i == Integer.MIN_VALUE) |
| return "-2147483648"; |
| |
| int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); |
| char[] buf = new char[size]; |
| |
| getChars(i, size, buf); |
| return new String(buf, true); |
| } |
# toUnsignedString(int i)
| public static String toUnsignedString(int i) { |
| return Long.toString(toUnsignedLong(i)); |
| } |
# getChars(int i, int index, char[] buf)
将整数 i 一个字符一个字符地写入 buf,index 是 buf 的起始位置。
| static void getChars(int i, int index, char[] buf) { |
| int q, r; |
| |
| int charPos = index; |
| |
| char sign = 0; |
| |
| |
| if (i < 0) { |
| sign = '-'; |
| i = -i; |
| } |
| |
| |
| |
| |
| while (i >= 65536) { |
| q = i / 100; |
| |
| |
| r = i - ((q << 6) + (q << 5) + (q << 2)); |
| i = q; |
| buf [--charPos] = DigitOnes[r]; |
| buf [--charPos] = DigitTens[r]; |
| } |
| |
| |
| |
| for (;;) { |
| |
| |
| |
| |
| q = (i * 52429) >>> (16+3); |
| r = i - ((q << 3) + (q << 1)); |
| buf [--charPos] = digits [r]; |
| i = q; |
| if (i == 0) break; |
| } |
| |
| if (sign != 0) { |
| buf [--charPos] = sign; |
| } |
| } |
# stringSize(int x)
获取整型 x 转为 string 之后的字符串长度。
| static int stringSize(int x) { |
| for (int i=0; ; i++) |
| if (x <= sizeTable[i]) |
| return i+1; |
| } |
# parseInt(String s, int radix)
将字符串 s 以 radix 为基数转换为有符号整型数。如果发生以下任何一种情况,则抛出类型 NumberFormatException
的异常:
- 第一个参数是 null 或是长度为零的字符串。
- 基数小于
Character.MIN_RADIX
或大于 Character.MAX_RADIX
。 - 字符串的任何字符不是指定基数的数字,除了第一个字符可能是减号或加号,前提是字符串长度大于 1.
- 由字符串表示的值不是 int 类型的值。
| public static int parseInt(String s, int radix) throws NumberFormatException { |
| |
| if (s == null) { |
| throw new NumberFormatException("null"); |
| } |
| |
| if (radix < Character.MIN_RADIX) { |
| throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); |
| } |
| |
| if (radix > Character.MAX_RADIX) { |
| throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); |
| } |
| |
| int result = 0; |
| boolean negative = false; |
| int i = 0, len = s.length(); |
| int limit = -Integer.MAX_VALUE; |
| int multmin; |
| int digit; |
| |
| if (len > 0) { |
| char firstChar = s.charAt(0); |
| |
| if (firstChar < '0') { |
| if (firstChar == '-') { |
| negative = true; |
| limit = Integer.MIN_VALUE; |
| } else if (firstChar != '+') |
| throw NumberFormatException.forInputString(s); |
| |
| if (len == 1) |
| throw NumberFormatException.forInputString(s); |
| i++; |
| } |
| multmin = limit / radix; |
| while (i < len) { |
| |
| |
| digit = Character.digit(s.charAt(i++),radix); |
| if (digit < 0) { |
| throw NumberFormatException.forInputString(s); |
| } |
| |
| if (result < multmin) { |
| throw NumberFormatException.forInputString(s); |
| } |
| |
| result *= radix; |
| |
| if (result < limit + digit) { |
| throw NumberFormatException.forInputString(s); |
| } |
| result -= digit; |
| } |
| } else { |
| throw NumberFormatException.forInputString(s); |
| } |
| return negative ? result : -result; |
| } |
# parseInt(String s)
将字符串 s 以 10 为基数转换为有符号整型数。
| public static int parseInt(String s) throws NumberFormatException { |
| return parseInt(s,10); |
| } |
# parseUnsignedInt(String s, int radix)
将字符串 s 以 radix 为基数转换为无符号整型数。如果发生以下任何一种情况,则抛出类型 NumberFormatException
的异常:
- 第一个参数是 null 或是长度为零的字符串。
- 基数小于
Character.MIN_RADIX
或大于 Character.MAX_RADIX
。 - 字符串的任何字符不是指定基数的数字,除了第一个字符可能是加号,前提是字符串长度大于 1.
- 由字符串表示的值大于最大的无符号整数值231−1。
| public static int parseUnsignedInt(String s, int radix) |
| throws NumberFormatException { |
| if (s == null) { |
| throw new NumberFormatException("null"); |
| } |
| |
| int len = s.length(); |
| if (len > 0) { |
| char firstChar = s.charAt(0); |
| if (firstChar == '-') { |
| throw new |
| NumberFormatException(String.format("Illegal leading minus sign " + "on unsigned string %s.", s)); |
| } else { |
| |
| |
| if (len <= 5 || |
| (radix == 10 && len <= 9) ) { |
| return parseInt(s, radix); |
| } else { |
| long ell = Long.parseLong(s, radix); |
| |
| if ((ell & 0xffff_ffff_0000_0000L) == 0) { |
| return (int) ell; |
| } else { |
| throw new NumberFormatException(String.format("String value %s exceeds " + "range of unsigned int.", s)); |
| } |
| } |
| } |
| } else { |
| throw NumberFormatException.forInputString(s); |
| } |
| } |
# parseUnsignedInt(String s)
将字符串 s 以 10 为基数转换为无符号整型数。
| public static int parseInt(String s) throws NumberFormatException { |
| return parseInt(s,10); |
| } |
# Integer valueOf(String s, int radix)
以 radix 为基数将字符串 s 转换为 Integer 类型。返回的 Integer 类型的数优先从缓存中获取。
| public static Integer valueOf(String s, int radix) throws NumberFormatException { |
| return Integer.valueOf(parseInt(s,radix)); |
| } |
# valueOf(String s)
以 10 为基数将字符串 s 转换为 Integer 类型。返回的 Integer 类型的数优先从缓存中获取。
| public static Integer valueOf(String s) throws NumberFormatException { |
| return Integer.valueOf(parseInt(s, 10)); |
| } |
# valueOf(int i)
将整型数 i 转换为 Integer 类型数。返回的 Integer 类型的数优先从缓存中获取。
| public static Integer valueOf(int i) { |
| |
| if (i >= IntegerCache.low && i <= IntegerCache.high) |
| return IntegerCache.cache[i + (-IntegerCache.low)]; |
| return new Integer(i); |
| } |
# byteValue()
返回 byte 类型的值。
| public byte byteValue() { |
| return (byte)value; |
| } |
# shortValue()
返回 short 类型的值。
| public short shortValue() { |
| return (short)value; |
| } |
# intValue()
返回 int 类型的值。
| public int intValue() { |
| return value; |
| } |
# longValue()
返回 long 类型的值。
| public long longValue() { |
| return (long)value; |
| } |
# floatValue()
返回 long 类型的值。
| public float floatValue() { |
| return (float)value; |
| } |
# doubleValue()
返回 double 类型的值。
| public double doubleValue() { |
| return (double)value; |
| } |
# toString()
返回 String 类型。
| public String toString() { |
| return toString(value); |
| } |
# hashCode()
返回哈希值。由此可见,Integer 对象的 hashcode 值是它的值。
| @Override |
| public int hashCode() { |
| return Integer.hashCode(value); |
| } |
# hashCode(int value)
返回 value 的哈希值。
| public static int hashCode(int value) { |
| return value; |
| } |
# equals(Object obj)
判断 obj 的 value 与调用该方法的对象的 value 是否相等。
| public boolean equals(Object obj) { |
| if (obj instanceof Integer) { |
| return value == ((Integer)obj).intValue(); |
| } |
| return false; |
| } |
# getInteger(String nm)
返回系统属性 nm 的整数值。
| public static Integer getInteger(String nm) { |
| return getInteger(nm, null); |
| } |
# getInteger(String nm, int val)
返回系统属性 nm 的整数值,如果为空,返回默认的 val 的 Integer。
| public static Integer getInteger(String nm, int val) { |
| Integer result = getInteger(nm, null); |
| return (result == null) ? Integer.valueOf(val) : result; |
| } |
# getInteger(String nm, Integer val)
返回系统属性 nm 的整数值,如果为空,返回默认的 val。
| public static Integer getInteger(String nm, Integer val) { |
| String v = null; |
| try { |
| v = System.getProperty(nm); |
| } catch (IllegalArgumentException | NullPointerException e) { |
| } |
| if (v != null) { |
| try { |
| return Integer.decode(v); |
| } catch (NumberFormatException e) { |
| } |
| } |
| return val; |
| } |
# decode(String nm)
将 nm 解码为 Integer。
| public static Integer decode(String nm) throws NumberFormatException { |
| int radix = 10; |
| int index = 0; |
| boolean negative = false; |
| Integer result; |
| |
| if (nm.length() == 0) |
| throw new NumberFormatException("Zero length string"); |
| char firstChar = nm.charAt(0); |
| |
| if (firstChar == '-') { |
| negative = true; |
| index++; |
| } else if (firstChar == '+') |
| index++; |
| |
| |
| |
| if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) { |
| index += 2; |
| radix = 16; |
| } |
| else if (nm.startsWith("#", index)) { |
| index ++; |
| radix = 16; |
| } |
| |
| else if (nm.startsWith("0", index) && nm.length() > 1 + index) { |
| index ++; |
| radix = 8; |
| } |
| |
| if (nm.startsWith("-", index) || nm.startsWith("+", index)) |
| throw new NumberFormatException("Sign character in wrong position"); |
| |
| try { |
| result = Integer.valueOf(nm.substring(index), radix); |
| result = negative ? Integer.valueOf(-result.intValue()) : result; |
| } catch (NumberFormatException e) { |
| |
| |
| String constant = negative ? ("-" + nm.substring(index)) |
| : nm.substring(index); |
| result = Integer.valueOf(constant, radix); |
| } |
| return result; |
| } |
# compareTo(Integer anotherInteger)
调用该方法的对象的 value 与参数中的对象的 value 相比较,this.value 比 anotherInteger.value 小返回 - 1,this.value 比 anotherInteger.value 大返回 1,this.value 等于 anotherInteger.value 返回 0。
| public int compareTo(Integer anotherInteger) { |
| return compare(this.value, anotherInteger.value); |
| } |
# compare(int x, int y)
比较 x 和 y 的大小,x 比 y 小返回 - 1,x 比 y 大返回 1,x 等于 y 返回 0。
| public static int compare(int x, int y) { |
| return (x < y) ? -1 : ((x == y) ? 0 : 1); |
| } |
# compareUnsigned(int x, int y)
将 x,y 视为无符号比较大小,因为负数符号位为 1,所以正数一定比负数小。
| public static int compareUnsigned(int x, int y) { |
| return compare(x + MIN_VALUE, y + MIN_VALUE); |
| } |
# toUnsignedLong(int x)
将 x 视为无符号转为 long 型。
| public static long toUnsignedLong(int x) { |
| return ((long) x) & 0xffffffffL; |
| } |
# divideUnsigned(int dividend, int divisor)
将 dividend 和 divisor 视为无符号相除。
| public static int divideUnsigned(int dividend, int divisor) { |
| |
| return (int)(toUnsignedLong(dividend) / toUnsignedLong(divisor)); |
| } |
# remainderUnsigned(int dividend, int divisor)
忽略符号取 dividend 除以 divisor 的余数。
| public static int remainderUnsigned(int dividend, int divisor) { |
| |
| return (int)(toUnsignedLong(dividend) % toUnsignedLong(divisor)); |
| } |
# highestOneBit(int i)
返回十进制数转为二进制最高位表示的十进制的数值。
| public static int highestOneBit(int i) { |
| |
| |
| i |= (i >> 1); |
| i |= (i >> 2); |
| i |= (i >> 4); |
| i |= (i >> 8); |
| i |= (i >> 16); |
| return i - (i >>> 1); |
| } |
# lowestOneBit(int i)
返回十进制数转为二进制最低位表示的十进制的数值,即忽略符号位之后最高位的数值。
| public static int lowestOneBit(int i) { |
| |
| return i & -i; |
| } |
# numberOfLeadingZeros(int i)
i 是整数,返回 i 的补码中最高位 0 的位数,从左向右数,下一位不是 0 的位数,范围是 0~32,0 代表没有。此方法很机智地采用了二分法的思想。numberOfLeadingZeros (8)=28。
| public static int numberOfLeadingZeros(int i) { |
| |
| if (i == 0) |
| return 32; |
| int n = 1; |
| |
| if (i >>> 16 == 0) { n += 16; i <<= 16; } |
| |
| if (i >>> 24 == 0) { n += 8; i <<= 8; } |
| |
| if (i >>> 28 == 0) { n += 4; i <<= 4; } |
| |
| if (i >>> 30 == 0) { n += 2; i <<= 2; } |
| |
| n -= i >>> 31; |
| return n; |
| } |
# numberOfTrailingZeros(int i)
i 是整数,返回 i 的补码中最低位 0 的位数。从右向左数,下一位不是 0 的位数。numberOfLeadingZeros (8)=3。
| public static int numberOfTrailingZeros(int i) { |
| |
| int y; |
| if (i == 0) return 32; |
| int n = 31; |
| y = i <<16; if (y != 0) { n = n -16; i = y; } |
| y = i << 8; if (y != 0) { n = n - 8; i = y; } |
| y = i << 4; if (y != 0) { n = n - 4; i = y; } |
| y = i << 2; if (y != 0) { n = n - 2; i = y; } |
| return n - ((i << 1) >>> 31); |
| } |
# bitCount(int i)
返回 i 的补码中值为 1 的位数的个数。
| public static int bitCount(int i) { |
| |
| i = i - ((i >>> 1) & 0x55555555); |
| |
| i = (i & 0x33333333) + ((i >>> 2) & 0x33333333); |
| |
| i = (i + (i >>> 4)) & 0x0f0f0f0f; |
| |
| i = i + (i >>> 8); |
| |
| i = i + (i >>> 16); |
| |
| return i & 0x3f; |
| } |
# rotateLeft(int i, int distance)
i 的二进制值循环左移 distance 位。普通左移会丢失最左边位数的数,但是循环左移最左边的数会循环到最右边。
| public static int rotateLeft(int i, int distance) { |
| return (i << distance) | (i >>> -distance); |
| } |
# rotateRight(int i, int distance)
i 的二进制值循环右移 distance 位。
| public static int rotateRight(int i, int distance) { |
| return (i >>> distance) | (i << -distance); |
| } |
# reverse(int i)
反转 i 的二进制值上每一位的数。
| public static int reverse(int i) { |
| |
| |
| i = (i & 0x55555555) << 1 | (i >>> 1) & 0x55555555; |
| |
| i = (i & 0x33333333) << 2 | (i >>> 2) & 0x33333333; |
| |
| i = (i & 0x0f0f0f0f) << 4 | (i >>> 4) & 0x0f0f0f0f; |
| |
| |
| |
| |
| |
| i = (i << 24) | ((i & 0xff00) << 8) | |
| ((i >>> 8) & 0xff00) | (i >>> 24); |
| return i; |
| } |
# signum(int i)
如果 i 为负数返回 - 1,如果 i 为正数返回 1,如果 i 为 0 返回 0。
| public static int signum(int i) { |
| |
| return (i >> 31) | (-i >>> 31); |
| } |
# reverseBytes(int i)
反转 i 的二进制值的每一个字节上数。
| public static int reverseBytes(int i) { |
| return ((i >>> 24) ) | |
| ((i >> 8) & 0xFF00) | |
| ((i << 8) & 0xFF0000) | |
| ((i << 24)); |
| } |
# sum(int a, int b)
返回 a 和 b 的和。
| public static int sum(int a, int b) { |
| return a + b; |
| } |
# max(int a, int b)
返回 a,b 中的最大值。
| public static int max(int a, int b) { |
| return Math.max(a, b); |
| } |
# min(int a, int b)
返回 a,b 中的最小值。
| public static int min(int a, int b) { |
| return Math.min(a, b); |
| } |
# 内部类
# IntegerCache
此内部类用来缓存部分大小的 Integer,默认范围 - 128-127。
| private static class IntegerCache { |
| static final int low = -128; |
| static final int high; |
| static final Integer cache[]; |
| |
| static { |
| |
| int h = 127; |
| String integerCacheHighPropValue = |
| sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); |
| if (integerCacheHighPropValue != null) { |
| try { |
| int i = parseInt(integerCacheHighPropValue); |
| |
| i = Math.max(i, 127); |
| |
| h = Math.min(i, Integer.MAX_VALUE - (-low) -1); |
| } catch( NumberFormatException nfe) { |
| |
| } |
| } |
| high = h; |
| |
| |
| cache = new Integer[(high - low) + 1]; |
| int j = low; |
| for(int k = 0; k < cache.length; k++) |
| cache[k] = new Integer(j++); |
| |
| |
| assert IntegerCache.high >= 127; |
| } |
| |
| private IntegerCache() {} |
| } |