最近研究了一下Integer类,记录一下
概述
Integer是int基础数据类型的包装类,实际值是Integer的属性value ,继承自 Number类, 最大值 0x7fffffff ,最小值:0x80000000
Integer 有个内部缓存类IntegerCache默认缓存 -128 ~ 127
当使用Integer.value(i) 时,优先取cache中的值;
Integer a = new Integer(1);
Integer b = new Integer(1);
Integer c = 1;
Integer d = 1;
Integer e = 128;
Integer f = 128;
System.out.println(a == b);
System.out.println(c == d);
System.out.println(e == f);
运行结果:
false
true
false
常用方法
hashCode()
返回就是Integer本身的值
@Override
public int hashCode() {
return Integer.hashCode(value);
}
public static int hashCode(int value) {
return value;
}
Integer.parserInt(String s, int radix) 字符串转不同进制的int
radix范围: 2<=radix<=36
代码:
public static int parseInt(String s, int radix) throws NumberFormatException {
/*
* WARNING: This method may be invoked early during VM initialization
* before IntegerCache is initialized. Care must be taken to not use
* the valueOf method.
*/
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) {
// 1
char firstChar = s.charAt(0);
if (firstChar < '0') { // Possible leading "+" or "-"
if (firstChar == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
// 2
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);
}
// 3
return negative ? result : -result;
}
1、首字符判断数字正负数,确定数值范围
2、从最高位开始,取值转换成数字,累计最终值,注意这里是做减法运算
因为负数比正数多1位。
result *= radix;
result -= digit;
Integer.toString(int i) 数字转字符串
parseInt的反向操作,这里是转成10进制数字的字符串
代码:
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);
}
1、如果是最小值,直接返回,简化计算
2、计算总字符串长度,负数+1位
3、利用字符数组存入每一位字符
4、返回字符串
再看getChars(i, size, 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;
}
// Generate two digits per iteration
while (i >= 65536) {
q = i / 100;
// really: r = i - (q * 100);
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
buf [--charPos] = DigitOnes[r];
buf [--charPos] = DigitTens[r];
}
// Fall thru to fast mode for smaller numbers
// assert(i <= 65536, i);
for (;;) {
q = (i * 52429) >>> (16+3);
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;
}
}
这里 ,
①r = i - ((q << 6) + (q << 5) + (q << 2)); 相当于 r = i - (q100), 乘法运算的实现其实也是位移,这里直接使用位移速度更快;
②当i<65536时,q = (i * 52429) >>> (16+3);这段代码其实就是q=i/10 ,其中 (double)52429/(1<<19)=0.10000038146972656也就是在int型的时候计算一个数的十分之1的精度是够的;
③r = i - ((q << 3) + (q << 1)); 这里没有使用i%10,而是 i - (q10)
当i>=65536时,一次性取两个字符,优化了速度
stringSize(int x)
获取数字的长度,这里使用了一个数组,数组里面放了int每位数上的最大值
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
99999999, 999999999, Integer.MAX_VALUE };
// Requires positive x
static int stringSize(int x) {
for (int i=0; ; i++)
if (x <= sizeTable[i])
return i+1;
}