1. Integer的緩存技術
JDK1.5後Java引入了自動裝箱和自動拆箱技術,
Integer ina = 2;
Integer inb = 2;
System.out.println("裝箱後 "+(ina == inb));
Integer inc = 200;
Integer ind = 200;
System.out.println("裝箱後"+(inc == ind));
上面的執行結果分別爲true和false.爲什麼兩次裝箱結果不同呢?這是由於java的Integer在實現時有這樣一個內部類,
private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[];
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}
不難發現,-128到127之間的整數會在第一次使用時(類加載時)被放在一個叫cache的數組裏。當然這個大小區間可以改變JVM的設置來改變。
並且,Integer的靜態方法valueOf(int i)也使用了該數組。
public static Integer valueOf(int i) {
assert IntegerCache.high >= 127;
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
2. 一個整數在給定進制的字符串表示
toString(int i , int radix)
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));
以上是核心代碼,實際上在JDK源碼裏是統一化成負數處理。筆者覺得這不符合習慣,此次改爲統一化爲正數處理。至於這裏爲什麼數組長度爲33也是明顯的,因爲java裏int有32位,加上可能的符號位。
3. 一個整數的長度
static int stringSize(int x)
這個函數不是個public權限的函數,作爲內部工具方法使用。
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;
}
這個方法的實現是很巧妙的,避免除法、求餘等,判斷條件簡單,效率高(採用靜態field分析,而不是負責邏輯判斷可以明顯提高效果)。(int 最大長只有10)
4. toString()的實現
可能有人覺得上面的toString(int i, int radix)已經是通用算法了,但是JDK在並沒有這樣(即radix是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);
}
這個算法的核心是getChars的實現,即將一個整數高效地逐位存入一個char數組中。
static void getChars(int i, int index, char[] buf) {
int q, r;
int charPos = index;
char sign = 0;
if (i < 0) {
sign = '-';
i = -i;
}
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];
}
這個處理大數部分,其中DigitOnes和DigitTens分別如下,
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',
} ;
上面while部分的思想是,DigitOnes是代表個位,DigitTens代表十位,每次r可以迭代兩位(r就是除以100的餘數)。至於移位運算,是爲了提高運算速度,q*100 = q*(2^6) +q*(2^5) + q*(2^2) = 64q+32q+4q.
然後對小數採用更快速的方式,
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;
}
上面操作的作用是q得到i截斷個位的值.(q = i / 10 ).至於採用上述複雜的移位的目的是提高速度(>>>無符號右移).