Java常量池詳解之Integer緩衝

來源:互聯網
上載者:User

標籤:off   ecif   封裝   pack   form   public   常量池   request   fse   

一個Java question,求輸出結果
 
public class IntegerTest {public static void main(String[] args) {objPoolTest();}public static void objPoolTest() {Integer i1 = 40;Integer i2 = 40;Integer i3 = 0;Integer i4 = new Integer(40);Integer i5 = new Integer(40);Integer i6 = new Integer(0);System.out.println("i1=i2 \t" + (i1 == i2));System.out.println("i1=i2+i3 \t" + (i1 == i2 + i3));System.out.println("i4=i5 \t" + (i4 == i5));System.out.println("i4=i5+i6 \t" + (i4 == i5 + i6));System.out.println();}}
 

輸出結果是

 

i1=i2 truei1=i2+i3 truei4=i5 falsei4=i5+i6 true
看起來比較Easy的問題,但是Console輸出的Result和我們所想的確恰恰相反,我們就疑惑了,這是為什麼咧?
最後通過網上搜尋得知Java為了提高效能提供了和String類一樣的對象池機制,當然Java的八種基本類型的封裝類(Packaging Type)也有對象池機制。Integer i1=40;Java在編譯的時候會執行將代碼封裝成Integer i1=Integer.valueOf(40);通過查看Source Code發現
   /**     * Returns a <tt>Integer</tt> instance representing the specified     * <tt>int</tt> value.     * If a new <tt>Integer</tt> instance is not required, this method     * should generally be used in preference to the constructor     * {@link #Integer(int)}, as this method is likely to yield     * significantly better space and time performance by caching     * frequently requested values.     *     * @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);    }

 

Integer.valueOf()中有個內部類IntegerCache(類似於一個常量數組,也叫對象池),它維護了一個Integer數組cache,長度為(128+127+1)=256;Integer類中還有一個Static Block(靜態塊)
static {        for(int i = 0; i < cache.length; i++)        cache[i] = new Integer(i - 128);    }

 

從這個靜態塊可以看出,Integer已經預設建立了數值【-128-127】的Integer快取資料。所以使用Integer i1=40時,JVM會直接在該在對象池找到該值的引用。   也就是說這種方式聲明一個Integer對象時,JVM首先會在Integer對象的緩衝池中尋找有木有值為40的對象,如果有直接返回該對象的引用;如果沒有,則使用New keyword建立一個對象,並返回該對象的引用地址。因為Java中【==】比較的是兩個對象是否是同一個引用(即比較記憶體位址),i2和i2都是引用的同一個對象,So i1==i2結果為”true“;而使用new方式建立的i4=new Integer(40)、i5=new Integer(40),雖然他們的值相等,但是每次都會重新Create新的Integer對象,不會被放入到對象池中,所以他們不是同一個引用,輸出false。  對於i1==i2+i3、i4==i5+i6結果為True,是因為,Java的數學計算是在記憶體棧裡操作的,Java會對i5、i6進行拆箱操作,其實比較的是基本類型(40=40+0),他們的值相同,因此結果為True。    好了,我想說道這裡大家應該都會對Integer對象池有了更進一步的瞭解了吧,我在諾諾的問一句如果把40改為400猜猜會輸出什嗎?
 
i1=i2 falsei1=i2+i3 truei4=i5 falsei4=i5+i6 true
這是因為Integer i1=400,Integer i2=400他們的值已經超出了常量池的範圍,JVM會對i1和i2各自建立新的對象(即Integer i1=new Integer(400)),所以他們不是同一個引用。
 
 
轉載自:http://www.tuicool.com/articles/aqyaYnm

Java常量池詳解之Integer緩衝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.