一、类说明
Integer类是用来包装原始类型int的类,用以表示原始类型int的面向对象的类。类中包含了原始类型int的值及相应的操作。
二、源码解析
package java.lang; import java.util.Properties; public final class Integer extends Number implements Comparable<Integer> { public static final int MIN_VALUE = 0x80000000; //Integer中所能存储的最大值,即231-1。 public static final int MAX_VALUE = 0x7fffffff; //Integer中所能存储的最小值,即2-31。 public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int"); //原始类型int的Class实例。 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' }; //所有可能的将数字表示为字符串的字符集合。 //返回第二个参数所指定的进制数的第一个参数的字符串表示形式 public static String toString(int i, int radix) { if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) radix = 10; /* Use the faster version */ 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)); } public static String toHexString(int i) { return toUnsignedString(i, 4); } public static String toOctalString(int i) { return toUnsignedString(i, 3); } public static String toBinaryString(int i) { return toUnsignedString(i, 1); } //转换int类型为无符号的字符串 private static String toUnsignedString(int i, int shift) { char[] buf = new char[32]; int charPos = 32; int radix = 1 << shift; //使用左移操作符,提升性能。 int mask = radix - 1; do { buf[--charPos] = digits[i & mask]; //使用&位移操作符得出每一位上的数字。 i >>>= shift; //移除已经得到的数字 } while (i != 0); return new String(buf, charPos, (32 - charPos)); } //十位数上的字符集 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', } ; //个位数上的字符集 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', } ; public static String toString(int i) { if (i == Integer.MIN_VALUE) return "-2147483648"; int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i); //如果i为负数,则字符串长度要加1。 char[] buf = new char[size]; getChars(i, size, buf); //获取i中字符的集合。 return new String(buf, true); } static void getChars(int i, int index, char[] buf) { int q, r; int charPos = index; char sign = 0; if (i < 0) { //如果i为负数,则设置i的符号字符为'-'。 sign = '-'; i = -i; } // Generate two digits per iteration while (i >= 65536) { //如果i大于65536,则每一次都获取十位和个位上的数字对应的字符。 q = i / 100; // really: r = i - (q * 100); r = i - ((q << 6) + (q << 5) + (q << 2)); //每次获得i的最后两位数 i = q; buf [--charPos] = DigitOnes[r]; //存储r中在个位数集合中对应的字符 buf [--charPos] = DigitTens[r]; //存储r中在十位数集合中对应的字符 } // Fall thru to fast mode for smaller numbers // assert(i <= 65536, i); for (;;) { //i<65536的情况 q = (i * 52429) >>> (16+3); r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... //每次获得i的最后两位数 buf [--charPos] = digits [r]; i = q; if (i == 0) break; } if (sign != 0) { buf [--charPos] = sign; //设置符号 } } //定义sizeTable表示int中每个位数中最大的数,用于简便确定int数的长度。 final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999, 99999999, 999999999, Integer.MAX_VALUE }; //使用上面的sizeTable定义来确定int数的字符串表示长度。 static int stringSize(int x) { for (int i=0; ; i++) if (x <= sizeTable[i]) return i+1; } 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(); //i表示当前遍历的s的位数 int limit = -Integer.MAX_VALUE; //设置最小值为负的Integer的最大值 int multmin; int digit; if (len > 0) { //如果字符串长度大于0,则进行转换 char firstChar = s.charAt(0); //获取第一位字符 //在第一位字符小于'0'的情况下,判定是否含有符号'+'、'-' if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { //为负数的情况 negative = true; limit = Integer.MIN_VALUE; //不能小于Integer的最小值 } else if (firstChar != '+') //不为'-'时,如果不为'+',则表示符号错误。 throw NumberFormatException.forInputString(s); //如果字符串的长度等于1,且这个字符小于'0',则表示该字符串仅为一个符号,抛出异常 if (len == 1) // Cannot have lone "+" or "-" throw NumberFormatException.forInputString(s); i++; } multmin = limit / radix; while (i < len) { //进行进制的转换 // Accumulating negatively avoids surprises near MAX_VALUE 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; //根据符号返回正数还是负数 } public static int parseInt(String s) throws NumberFormatException { return parseInt(s,10); } public static Integer valueOf(String s, int radix) throws NumberFormatException { return Integer.valueOf(parseInt(s,radix)); } public static Integer valueOf(String s) throws NumberFormatException { return Integer.valueOf(parseInt(s, 10)); } //内部字符缓存类 private static class IntegerCache { //缓存的下界,-128,不可变 static final int low = -128; //缓存上界,暂为null static final int high; static final Integer cache[]; //利用数组来缓存 static { // high value may be configured by property // 缓存上届,可以通过JVM属性来配置 int h = 127; String integerCacheHighPropValue = sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); //获取 if (integerCacheHighPropValue != null) { int i = parseInt(integerCacheHighPropValue); i = Math.max(i, 127); // Maximum array size is Integer.MAX_VALUE h = Math.min(i, Integer.MAX_VALUE - (-low) -1); } high = h; //获取Integer中所有能保存的数据 cache = new Integer[(high - low) + 1]; int j = low; //缓存所有Integer的数据 for(int k = 0; k < cache.length; k++) cache[k] = new Integer(j++); } private IntegerCache() {} } public static Integer valueOf(int i) { assert IntegerCache.high >= 127; //断言Integer缓存中的上届必须大于或等于127 if (i >= IntegerCache.low && i <= IntegerCache.high) //如果i在Integer缓存中,则直接取出 return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); //否则,直接创建一个实例 } }