Integer類
public final class Integer extends Number implements Comparable<Integer>{}
Integer是用final聲明的常量類,不能被任何類所繼承。並且Integer類繼承了Number類和實現了Comparable接口。Number類是一個抽象類,8中基本數據類型的包裝類除了Character和Boolean沒有繼承該類外,剩下的都繼承了Number類,該類的方法用於各種數據類型的轉換。Comparable接口就一個compareTo方法,用於元素之間的大小比較,下面會對這些方法詳細展開介紹。
構造方法
public Integer(int value) {
this.value = value;
}
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10);
}
toString() toString(int i) toString(int i, int radix)
public static String toString(int i, int radix) {
// 默認十進制
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)
radix = 10;
/* Use the faster version */
if (radix == 10) {
return toString(i);
}
//
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));
}
parseInt()
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);
// 字符串的長度爲1但是又不是數字, 那肯定就出錯了
if (len == 1) // Cannot have lone "+" or "-"
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
/*
* 下面的過程其實很好理解, 以8進制的"534"爲例
* (-5*8-3)*8-4 = -348, 根據符號位判斷返回的是348
*/
while (i < len) {
// Accumulating negatively avoids surprises near MAX_VALUE
// 除了前面的判斷這裏的也有點複雜, 因爲要考慮到各種進位
// 這個將i位置上的字符根據基數轉爲實際的值, A->11
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; // 根據符號位來判斷返回哪一個
}
-
沒想通的一點是,常理來說(至少我是這樣的哈)是考慮用加法,然後再根據符號位判斷正負,但是源碼中用的是減法。這點沒想通是爲什麼,雖然也沒差,感覺怪怪的。
-
digit = Character.digit(s.charAt(i++),radix);
這裏的函數調用裏面的代碼也挺多的。根據該位上的字符和基數來得到對應的數字。
valueOf(int i)
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
可以看到這裏使用到了IntegerCache緩存,IntegerCache默認緩存-128~127之間的Integer。IntegerCache是Integer類的靜態內部類。
private static class IntegerCache {
static final int low = -128; //默認low=-128
static final int high; //high可以配置,通過 VM 參數-XX:AutoBoxCacheMax=<size>
//high可以配置,所以默認緩存-128~127,但是也可以緩存另外的常用數。
static final Integer cache[]; //緩存數組
//靜態代碼塊,Integer類加載時就緩存。
static {
// high value may be configured by property
int h = 127; //默認127
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high"); //讀取VM參數配置。
if (integerCacheHighPropValue != null) {
try {
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); //防止越界
} 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; //保證[-128, 127]在緩存範圍內。
}
private IntegerCache() {}
}