轉(zhuǎn)載 http://hxraid.iteye.com/blog/614440
我們首先來(lái)看一段代碼:
- Integer?i= 100 ; ??
- Integer?j= 100 ; ??
- System.out.println(i==j);?? //true ??
- Integer?i= 200 ; ??
- Integer?j= 200 ; ??
- System.out.println(i==j);?? //false ??
Integer i=100;
Integer j=100;
System.out.println(i==j); //true
Integer i=200;
Integer j=200;
System.out.println(i==j); //false
?
差不多的兩段代碼,怎么結(jié)果完全不同呢。我們分兩步來(lái)說(shuō)明這個(gè)問(wèn)題:
?
首先 Integer i=100; 編譯器會(huì)自動(dòng)將int類型常數(shù)100包裝成Interger,采用的是Integer.valueOf(100); 這個(gè)方法。
?
然后我們看看valueOf(int)這個(gè)方法的源代碼:
- ? /* ?
- ??*?返回一個(gè)表示指定的?int?值的?Integer?實(shí)例。如果不需要新的?Integer?實(shí)例,則 ?
- ??*?通常應(yīng)優(yōu)先使用該方法,而不是構(gòu)造方法?Integer(int),因?yàn)樵摲椒ㄓ锌赡芡ㄟ^(guò) ?
- ??*?緩存經(jīng)常請(qǐng)求的值而顯著提高空間和時(shí)間性能。? ?
- ??*?@param??i?an?<code>int</code>?value. ?
- ??*?@return?a?<tt>Integer</tt>?instance?representing?<tt>i</tt>. ?
- ??*?@since??1.5 ?
- ??*/ ??
- public ? static ?Integer?valueOf( int ?i)?{ ??
- ?????? final ? int ?offset?=? 128 ; ??
- ?????? if ?(i?>=?- 128 ?&&?i?<=? 127 )?{? //?must?cache? ??
- ???????? return ?IntegerCache.cache[i?+?offset]; ??
- ?????} ??
- ????? return ? new ?Integer(i); ??
- } ??
- ??
- ? /* ?
- ??*?IntegerCache內(nèi)部類 ?
- ??*?其中cache[]數(shù)組用于存放從-128到127一共256個(gè)整數(shù) ?
- ??*/ ??
- private ? static ? class ?IntegerCache?{ ??
- ???? private ?IntegerCache(){} ??
- ??
- ???? static ? final ?Integer?cache[]?=? new ?Integer[-(- 128 )?+? 127 ?+? 1 ]; ??
- ??
- ???? static ?{ ??
- ???????? for ( int ?i?=? 0 ;?i?<?cache.length;?i++) ??
- ????????cache[i]?=? new ?Integer(i?-? 128 ); ??
- ????} ??
- }??
/*
* 返回一個(gè)表示指定的 int 值的 Integer 實(shí)例。如果不需要新的 Integer 實(shí)例,則
* 通常應(yīng)優(yōu)先使用該方法,而不是構(gòu)造方法 Integer(int),因?yàn)樵摲椒ㄓ锌赡芡ㄟ^(guò)
* 緩存經(jīng)常請(qǐng)求的值而顯著提高空間和時(shí)間性能。
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}
/*
* IntegerCache內(nèi)部類
* 其中cache[]數(shù)組用于存放從-128到127一共256個(gè)整數(shù)
*/
private static class IntegerCache {
private IntegerCache(){}
static final Integer cache[] = new Integer[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Integer(i - 128);
}
}
?
原來(lái)如此, 當(dāng)-128=<i<=127的時(shí)候,返回的是IntegerCache中的數(shù)組的值;當(dāng) i>127 或 i<-128 時(shí),返回的是Integer類對(duì)象。 這就好解釋:
?
- Integer?i= 100 ; ??
- Integer?j= 100 ; ??
- System.out.println(i==j);?? //(1) ??
Integer i=100;
Integer j=100;
System.out.println(i==j); //(1)
此時(shí)的 i=IntegerCache.cache[228],因此 Integer引用i中存儲(chǔ)的是cache數(shù)組第228號(hào)元素的地址。同理j也是同一個(gè)cache數(shù)組的第228號(hào)元素的地址(因?yàn)閏ache是Integer的靜態(tài)數(shù)組,只有一個(gè))。i==j比較的是引用地址,因此相等。
- Integer?i= 200 ; ??
- Integer?j= 200 ; ??
- System.out.println(i==j);?? //(2) ??
Integer i=200;
Integer j=200;
System.out.println(i==j); //(2)
此時(shí)的 i=new Integer(200);? 同樣j=new Integer(200) 。兩次都在堆中開(kāi)辟了Integer的對(duì)象。i 和 j 中存儲(chǔ)的堆得對(duì)象地址是完全不同的。i==j 自然不相等了。
?
那么這樣做有什么意義呢? 我們來(lái)看看API的解釋:
- 返回一個(gè)表示指定的? int ?值的?Integer?實(shí)例。如果不需要新的?Integer?實(shí)例, ??
- 則通常應(yīng)優(yōu)先使用該方法,而不是構(gòu)造方法?Integer( int ),因?yàn)樵摲椒ㄓ锌赡???
- 通過(guò)緩存經(jīng)常請(qǐng)求的值而顯著提高空間和時(shí)間性能。???
返回一個(gè)表示指定的 int 值的 Integer 實(shí)例。如果不需要新的 Integer 實(shí)例,
則通常應(yīng)優(yōu)先使用該方法,而不是構(gòu)造方法 Integer(int),因?yàn)樵摲椒ㄓ锌赡?通過(guò)緩存經(jīng)常請(qǐng)求的值而顯著提高空間和時(shí)間性能。
假如我們?cè)诰幊虝r(shí)大量需要值為100的Integer對(duì)象,如果只能通過(guò)new來(lái)創(chuàng)建的話。是不是需要在堆中開(kāi)辟大量值一樣的Integer對(duì)象呢!這是相當(dāng)不劃算的。既然如此,Java中的字符串常量池的應(yīng)用是不是可以提醒我們點(diǎn)什么呢?是的,
IntegerCache.cache就相當(dāng)于這樣一個(gè)字符串常量池。
當(dāng)我們需要Integer i=100的時(shí)候,直接從cache中取出第[100+128]號(hào)元素的地址賦值引用i,再次需要Integer j=100時(shí),還是直接去這個(gè)地址賦給j .....
是不是省去了再堆中不停的創(chuàng)建對(duì)象的代價(jià)了(空間,時(shí)間上的消耗都很大)。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元

