title: Integer源碼分析
date: 2017-09-11 15:07:46
tags: java
categories: java
Integer繼承了Number類,并且實現(xiàn)了Comparable
Number類
Number類是所有數(shù)字類的基類,它是一個抽象類峭梳,并且實現(xiàn)序列化的接口
其中提供了四種抽象方法,是將對應(yīng)的value轉(zhuǎn)換成對類型的值返回
intValue();
longValue();
floatValue();
doubleValue();
Integer類
Integer實現(xiàn)了Number中的所有接口(我覺得只是提供了一種安全地的轉(zhuǎn)型)
- 構(gòu)造方法
Integer只提供了兩種構(gòu)造方法尚卫,一種是初始化數(shù)字四瘫,一種是使用String通過內(nèi)部的parseInt將字符轉(zhuǎn)換成數(shù)字
public Integer(int value)
public Integer(String s) throws NumberFormatException
- to系列方法
- toString方法
Integer類中西饵,toString方法有兩個蜈膨,其中一個是重寫了Object的toString方法悼沿, 另外兩個是靜態(tài)方法
#1 public String toString();
#2 public static String toString(int i);
#3 public static String toString(int i, int radix)
重寫的toString是調(diào)用了#2方法
#2 的實現(xiàn)
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
// 通過stringSize的方法獲取傳入i的長度茸歧,如果是負(fù)數(shù)則多一位
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
// 創(chuàng)建一個長度為size的字符數(shù)組
char[] buf = new char[size];
// 獲取i的字符形式給塞到buf中
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;
// 如果i小于0則要轉(zhuǎn)換為整數(shù),記錄下轉(zhuǎn)換前的符號
if (i < 0) {
sign = '-';
i = -i;
}
// 如果i大于或者等于65536需要显沈,獲取最后兩位來進(jìn)行處理
while (i >= 65536) {
q = i / 100;
r = i - ((q << 6) + (q << 5) + (q << 2));
i = q;
// 各位
buf [--charPos] = DigitOnes[r];
// 十位
buf [--charPos] = DigitTens[r];
}
// 死循環(huán)處理i软瞎,直到i為0退出循環(huán)
for (;;) {
// q、r兩個計算拉讯,是獲取i的最后一位涤浇,然后把獲取出來的數(shù)字獲取到預(yù)先定義的digits字符數(shù)組中的字符,并存入到buf中
q = (i * 52429) >>> (16+3);
r = i - ((q << 3) + (q << 1));
buf [--charPos] = digits [r];
i = q;
if (i == 0) break;
}
// 如果不是0魔慷,則把上面的符號位加到上面去
if (sign != 0) {
buf [--charPos] = sign;
}
}
#3 的實現(xiàn)
#3 提供了進(jìn)制轉(zhuǎn)換功能
public static String toString(int i, int radix) {
// 如果進(jìn)制不在2到36進(jìn)制之類只锭,則默認(rèn)為10進(jìn)制
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
// 如果是十進(jìn)制,直接調(diào)用toString方法返回
if (radix == 10) {
return toString(i);
}
char buf[] = new char[33];
// 記錄符號位
boolean negative = (i < 0);
int charPos = 32;
// 將正數(shù)變?yōu)樨?fù)數(shù)
if (!negative) {
i = -i;
}
while (i <= -radix) {
// 取余
buf[charPos--] = digits[-(i % radix)];
i = i / radix;
}
// 將無需處理的丟到數(shù)組的最后
buf[charPos] = digits[-i];
// 還原符號位
if (negative) {
buf[--charPos] = '-';
}
return new String(buf, charPos, (33 - charPos));
}
- toHexString院尔、toOctalString蜻展、toBinaryString
這些都是調(diào)用toUnsignedString0實現(xiàn)的(騷代碼喉誊,看不懂)
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);
}
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;
}
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;
}
- parse系列方法
它注釋里面給了一個警告,如果初始化時提早使用valueof纵顾,那么high可能為0
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) {
// 取傳入s的第一個字符
char firstChar = s.charAt(0);
// 比較字符的ASCALL碼伍茄,+ 或者 - 的都比數(shù)字小
if (firstChar < '0') {
// 如果是負(fù)數(shù),負(fù)數(shù)標(biāo)記施逾,limit設(shè)置為最小值
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++;
}
multmin = limit / radix;
// i可以看做字符串的指針
while (i < len) {
// 將digit轉(zhuǎn)換為對應(yīng)的進(jìn)制數(shù)的數(shù)值
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
// 每循環(huán)一次都需要乘以進(jìn)制數(shù)敷矫,相當(dāng)于擴(kuò)大radix倍
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
// 因為防止溢出,所以結(jié)果集使用負(fù)數(shù)存儲
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
return negative ? result : -result;
}
好像Integer常用的也就這些方法汉额,它其中很多方法是調(diào)用Long中的方法執(zhí)行的曹仗,或者是轉(zhuǎn)換成無符號的整數(shù)