JDK源码之Integer
1、Integer类简介Integer是int的包装类,被final修饰,不能被其他类继承,继承Number类,Integer重写了相关方法,如longValue(),floatValue(),doubleValue()等等。Integer类实现了Compare接口。
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;<br> //标志是否为负数 默认为false
boolean negative = false;<br> //遍历初始位置i=0 , 字符串长度
int i = 0, len = s.length();
int limit = -Integer.MAX_VALUE;
int multmin;
int digit;
//字符串长度需要大于0 否者直接抛出异常
if (len > 0) {<br> //字符串第一个字符
char firstChar = s.charAt(0);<br> //第一个字符是否小于'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++;
}<br> // 计算乘法前的最小值,用于后续溢出检查
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 && iCharacter.MAX_RADIX) radix = 10; /* Use the faster version */ if (radix == 10) { return toString(i); } char buf[] = new char; boolean negative = (i < 0); int charPos = 32; if (!negative) { i = -i; } while (i = 2^16 while (i >= 65536) { q = i / 100; // 等价于 r = i - (q * 100); r = i - ((q
页:
[1]