【JDK】:java.lang.Integer原始碼解析
本文對JDK8中的java.lang.Integer包裝類的部分數值快取技術、valueOf()、stringSize()、toString()、getChars()、parseInt()等進行簡要分析。
Integer快取
先來看一段程式碼:
Integer a1 = Integer.valueOf(13);
Integer a2 = Integer.valueOf(13);
Integer a3 = Integer.valueOf(133);
Integer a4 = Integer.valueOf(133);
System.out.println(a1 == a2); // 輸出 true
System.out.println(a3 == a4); // 輸出 false
兩個輸出語句具有不同的輸出,在於Integer使用了一個靜態內部類(巢狀類),裡面包含了一個快取陣列cache[],預設情況下,[-128, 127]之間的整數會在第一次使用時(類載入時)被自動裝箱,放在cache[]數組裡。區間的上限值high設定JVM引數-XX:AutoBoxCacheMax來改變,預設情況下引數為127(byte型別的範圍),儲存在java.lang.Integer.IntegerCache.high屬性中。
// 靜態內部類實現[-128, 127]的快取
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high 值通過JVM進行設定,預設為127
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high" );
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// 最大快取上限 Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
// range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
}
private IntegerCache() {}
}
而使用Integer.valueOf()進行構造時,就使用了cache[]快取陣列。因此使用該方法構造的Integer物件如果在快取區間內,會直接返回cache[]陣列內的相應的引用,自然就是同一個物件;否則將生成一個全新的Integer物件。與此對應的,如果使用建構函式Integer()直接構造,根本沒有使用到快取陣列,生成的一定是全新的Integer物件。因此使用Integer.valueOf()構造能夠節省資源,提高效率。
// 使用cache[]陣列構造
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
// 使用建構函式構造
public Integer(int value) {
this.value = value;
}
stringSize()
這個函式不是個public許可權的函式,作為內部工具方法使用。這個方法的實現是很巧妙的,避免除法、求餘等,判斷條件簡單,效率高(採用靜態field分析,而不是負責邏輯判斷可以明顯提高效果)。(int 最大長只有10)
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;
}
toString(int i , int radix)
一個整數在給定進位制的字串表示。
public static String toString(int i, int radix) {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* 如果是10進位制,使用更加快速的轉換方式 */
if (radix == 10) {
return toString(i);
}
char buf[] = new char[33];
boolean negative = (i < 0);
int charPos = 32; // int佔4個位元組,32bit
// 以負數為基準進行處理
if (!negative) {
i = -i;
}
// 程式碼的簡潔!! radix為進位制,最小為2,最高位36
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));
}
上面的程式碼使用了一個final static 的字元陣列digits[],直接根據i與進位制radix的求餘結果從digits[]裡面取值,提高運算效率。
/** 所有可能代表數字的字元,最高支援36進位制
* All possible chars for representing a number as a String
*/
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'
};
toString()
toString()方法返回當前Integer物件的字串表示。可能有人覺得上面的toString(int i, int radix)已經是通用演算法了,但是JDK在並沒有這樣(即radix是10的情況),而是採用了效率更高的方法。
public String toString() {
return toString(value);
}
// toString()的呼叫方法
// 必須先判斷Integer.MIN_VALUE,因為getChars()方法中使用了i=-i
// 以負數為基準,對於i=Integer.MIN_VALUE將會產生溢位
public static String toString(int i) {
if (i == Integer.MIN_VALUE)
return "-2147483648";
// 獲取字串表示的字串長度,考慮了負數的符號位
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
// 將Integer數讀入到char[]陣列
char[] buf = new char[size];
getChars(i, size, buf);
return new String(buf, true);
}
這個演算法的核心是getChars的實現,即將一個整數高效地逐位存入一個char陣列中。
// 核心程式碼,從後向前將Integer讀入char[]字元表示陣列,如果i = MIN_VALUE將會發生大數溢位
// fail if i == Integer.MIN_VALUE
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index;
char sign = 0;
if (i < 0) {
sign = '-';
i = -i;
}
// 處理超過2的16次方的大數
// 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]; // 十位上的數字
}
// 處理小於2的16次方的數
// Fall thru to fast mode for smaller numbers
for (;;) {
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;
}
}
// 個位上的數字陣列
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',
} ;
getChars()分別對int型的高位的兩個位元組、低位的兩個位元組進行遍歷。while部分的思想是,DigitOnes是代表個位,DigitTens代表十位,每次r可以迭代兩位(r就是除以100的餘數),每次找出兩位數,這樣有效的減少了乘除法的次數。至於移位運算,是為了提高運算速度,q*100 = q*(2^6) +q*(2^5) + q*(2^2) = 64q+32q+4q.
for迴圈部分,q得到i截斷個位的值(q = i / 10 )。至於採用上述複雜的移位的目的是提高速度(>>>無符號右移)。q=i*(52429/216)/23≈≈i*0.1。因為這裡要用i*52429>>>16更精確的表示乘以十分之八的作用,而高位的兩個位元組的數再乘會溢位,所以原始碼裡進行了高位與低位用兩種方式分開迴圈。
parseInt()
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
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) {
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++;
}
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;
}
原始碼中注意的幾點:
- 所有的運算都是基於負數的。在toString也提到過,因為將Integer.MIN_VALUE直接變換符號會導致數值溢位。
- 溢位的判斷技巧。multmin = limit / radix 這個數的控制,可以在乘法計算之前可判斷計算之後是否溢位。同理,result < limit + digit 可在減法之前判斷計算後是否溢位。