1. 描述
- API:int的包裝類型,內(nèi)部包含了一個(gè)int字段。
- 關(guān)鍵字段:
private final int value; //和String類似,Integer內(nèi)部也是存儲(chǔ)了一個(gè)final的value
public static final int MIN_VALUE = 0x80000000; //Integer最小值-2^31
public static final int MAX_VALUE = 0x7fffffff; //Integer最大值2^31-1
2. 構(gòu)造函數(shù)
- 第一種 直接賦值:傳入的int直接賦值給value(代碼略)。
- 第二種 傳入String然后轉(zhuǎn)成int:
public Integer(String s) throws NumberFormatException {
this.value = parseInt(s, 10); //10是進(jìn)制
}
public static int parseInt(String s, int radix) throws NumberFormatException{
if (s == null) {
throw new NumberFormatException("null");
}
//進(jìn)制限制數(shù)最小2
if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
}
//進(jìn)制限制數(shù)最大36(0-9、a-z)
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) {
//拿第一個(gè)字符,如果是負(fù)號(hào)標(biāo)記,如果是正號(hào)或數(shù)字跳過,否則拋異常,下標(biāo)推后1
char firstChar = s.charAt(0);
//跳過第一位是數(shù)字的情況
if (firstChar < '0') {
if (firstChar == '-') {
//正負(fù)的標(biāo)記字段
negative = true;
limit = Integer.MIN_VALUE;
} else if (firstChar != '+')
throw NumberFormatException.forInputString(s);
if (len == 1)
throw NumberFormatException.forInputString(s);
i++;
}
multmin = limit / radix;
//計(jì)算方法是從最高位到最低位循環(huán),每次乘10然后相加,如十進(jìn)制的"123",result初始化0
//第一次循環(huán)"1" -> 1,result = result * 10 + 1 = 1
//第二次循環(huán)"2" -> 2,result = result * 10 + 2 = 12
//第三次循環(huán)"3" -> 3,result = result * 10 + 3 = 123,判斷符號(hào)返回result
while (i < len) {
//根據(jù)ASCII碼換算出數(shù)字,如10進(jìn)制'0'~'9'->0~9,16進(jìn)制'0'~'f'->0~16,超出返回-1
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
//結(jié)果不能越界
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
//result乘進(jìn)制數(shù)
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
//每次循環(huán)給最后一位賦值
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
//根據(jù)正負(fù)返回結(jié)果
return negative ? result : -result;
}
- 流程總結(jié):先判斷第一位是正負(fù),做標(biāo)記,然后從高位開始到低位循環(huán),每位都單獨(dú)轉(zhuǎn)換成int,再把之前結(jié)果乘10然后相加,最后計(jì)算正負(fù)得出結(jié)果。
3. equals()
- 先用instanceof判斷類型關(guān)系,然后用"=="判斷值是否相等(代碼很簡單略過)
4. hashCode()
- integer的重寫了hashCode()直接返回value字段本身
5. 自動(dòng)裝箱和拆箱
- 基本類型和包裝類型相互轉(zhuǎn)換時(shí),在編譯階段會(huì)進(jìn)行自動(dòng)裝箱、拆箱的優(yōu)化
//編譯前
Integer a = 128;
Integer b = new Integer(128);
int m = b;
-
通過javap -c反編譯仔細(xì)觀察一下
反編譯.jpg
//更直觀的看出編譯時(shí)到底做了什么優(yōu)化
Integer a = Integer.valueOf(128); //int -> Integer 自動(dòng)裝箱
Integer b = new Integer(129);
int m = b.intValue(); //Integer -> int 自動(dòng)拆箱
- Integer.valueOf() - 自動(dòng)裝箱
static final Integer cache[];
//-128~127都取是緩存的Integer對(duì)象,超出范圍就new一個(gè)新的
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
- intValue() - 自動(dòng)拆箱
//Integer轉(zhuǎn)int就是直接返回value
public int intValue() {
return value;
}
- 借助System.identityHashCode()進(jìn)行測試驗(yàn)證一下,為什么使用identityHashCode()跳轉(zhuǎn)http://www.itdecent.cn/p/7f0893577acf
//原代碼
Integer i = 10;
Integer j = 10;
Integer a = 128;
Integer b = 128;
int k = 10;
int kk = 128;
Integer m = new Integer(10);
Integer n = new Integer(10);
//輸出所有identityHashCode
System.out.println(System.identityHashCode(i));
System.out.println(System.identityHashCode(j));
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(b));
System.out.println(System.identityHashCode(k));
System.out.println(System.identityHashCode(kk));
System.out.println(System.identityHashCode(m));
System.out.println(System.identityHashCode(n));
System.out.println(k == m);
- 試著分析一下各個(gè)對(duì)象的hash碼:
- i - 654845766:之前說到這個(gè) i 是從IntegerCache里面拿的
- j - 654845766:從IntegerCache里面拿的所以和 i 相等
- a - 1712536284:不在-128~127之內(nèi),新建對(duì)象
- b - 2080166188:不在-128~127之內(nèi),新建對(duì)象,新對(duì)象和a不同
- k - 654845766:因?yàn)閕dentityHashCode()只接受Object不接受基本類型,所以會(huì)觸發(fā)裝箱就變成了Integer.valueOf(k)這個(gè)10是從IntegerCache里面拿的,所以和i、j相等
- kk - 1123225098:同k,裝箱后不在范圍內(nèi),新建對(duì)象,所以和其他不等
- m - 606548741:新建的對(duì)象
- n - 1528637575:新建對(duì)象
- k == m - true:int和integer比較時(shí)integer會(huì)觸發(fā)拆箱所以相等
- 裝箱、拆箱總結(jié):
- int轉(zhuǎn)integer就是裝箱Integer.valueOf(),integer轉(zhuǎn)int就是拆箱intValue()
- 字面量聲明會(huì)優(yōu)化裝箱,裝箱執(zhí)行時(shí)范圍在-128~127就讀緩存!
- PS:Integer為空,null.intValue()觸發(fā)拆箱時(shí)會(huì)報(bào)空指針異常。這個(gè)是技術(shù)群里某個(gè)同學(xué)提出的,不太明白為什么會(huì)提出來,不過也順便記下了。
