JDK源码之Integer
1、Integer类简介
Integer是int的包装类,被final修饰,不能被其他类继承,继承Number类,Integer重写了相关方法,如longValue(),floatValue(),doubleValue()等等。Integer类实现了Compare<Integer>接口。
2、源码
2.1、Integer类的属性
/** * int 类型能够表示的最小值 -2^31 * have, -2<sup>31</sup>. */ @Native public static final int MIN_VALUE = 0x80000000; /** * int 类型能够表示的最大值 2^31 * have, 2<sup>31</sup>-1. */ @Native public static final int MAX_VALUE = 0x7fffffff; /** * int 类型的 calss 实例 * {@code int}. * * @since JDK1.1 */ @SuppressWarnings("unchecked") public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");/** * 二进制补码形式下 int 的比特位数 * complement binary form. * * @since 1.5 */ @Native public static final int SIZE = 32; /** * 二进制补码下int的字节数/jdk1.8加入 * complement binary form. * * @since 1.8 */ public static final int BYTES = SIZE / Byte.SIZE;
2.2、Integer类的构造方法
public Integer(int value) { this.value = value; } public Integer(String s) throws NumberFormatException { this.value = parseInt(s, 10); }
共提供两个构造方法,一个参数为int,将int赋值给value。另外一个为String,通过parseInt方法将字符串转换为Integer。
2.3、parseInt()方法
public static int parseInt(String s, int radix) throws NumberFormatException { //字符串为空 直接抛出异常 if (s == null) { throw new NumberFormatException("null"); } //小于最小数2 抛出异常 MAX_RADIX = 2 计算机的基础进制 if (radix < Character.MIN_RADIX) { throw new NumberFormatException("radix " + radix + " less than Character.MIN_RADIX"); } //大于最大数36 抛出异常 MAX_RADIX = 36 表示0-9数字集a-z字母,共36字符 if (radix > Character.MAX_RADIX) { throw new NumberFormatException("radix " + radix + " greater than Character.MAX_RADIX"); } int result = 0;
//标志是否为负数 默认为false boolean negative = false;
//遍历初始位置i=0 , 字符串长度 int i = 0, len = s.length(); int limit = -Integer.MAX_VALUE; int multmin; int digit; //字符串长度需要大于0 否者直接抛出异常 if (len > 0) {
//字符串第一个字符 char firstChar = s.charAt(0);
//第一个字符是否小于'0'的ASCII的值,说明不符合十进制的数字的合法开头。有两个例外 是'+'或'-', if (firstChar < '0') { // Possible leading "+" or "-" if (firstChar == '-') { //如果是'-' negative = true; //负数标志变为true limit = Integer.MIN_VALUE; } else if (firstChar != '+') throw NumberFormatException.forInputString(s); //不是'+' 和 '-' 直接抛出异常 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; //使用负数累积 防止MIN_VALUE 的绝对值比 MAX_VALUE大 1的情况 int的取值范围是-2147483648 至 2147483647 } } else { throw NumberFormatException.forInputString(s); } return negative ? result : -result; //转换回对应的整数结果 }
2.4、valueOf()方法
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)); } // 根据传入的int 判断是否在缓存中,如果在,那么返回缓存中的数据,如果不在,则创建一个新的Integer对象返回 public static Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; return new Integer(i); }
使用valueOf()涉及到一个自动装箱的过程,就是把基本数据类型用对应的引用类型包装起来,使得它们由对象的特质,如double类型包装为Double,int包装成Integer类型。
//自动装箱 int a = 5; Integer b = a; // 编译器自动转换为Integer.valueOf(a) //自动拆箱 Integer b = new Integer(5); int a = b; //编译器自动转换为 b.intValue()
JDK 5 引入自动装箱/拆箱 JDK8:增强类型推断,减少显示装箱
2.5、intValue()方法
public int intValue() { return value; }
2.5、toString()方法
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 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); } static void getChars(int i, int index, char[] buf) { int q, r; int charPos = index; char sign = 0; if (i < 0) { sign = '-'; i = -i; } // 处理大数 i >= 2^16 while (i >= 65536) { q = i / 100; // 等价于 r = i - (q * 100); r = i - ((q << 6) + (q << 5) + (q << 2)); i = q; buf [--charPos] = DigitOnes[r]; buf [--charPos] = DigitTens[r]; } // 处理小数 // assert(i <= 65536, i); for (;;) {
//使用魔术数52429 优化除法 52429/2^19 约等于 1/10 q = (i * 52429) >>> (16+3); // q = i/10 r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ... buf [--charPos] = digits [r]; i = q; if (i == 0) break; }
//添加符号 if (sign != 0) { buf [--charPos] = sign; } }

浙公网安备 33010602011771号