Java自動(dòng)裝箱和拆箱以及裝箱時(shí)的緩存問(wèn)題(轉(zhuǎn)載)

概述:

  • 自動(dòng)裝箱:是把基本類型的值轉(zhuǎn)換為對(duì)應(yīng)的包裝類對(duì)象, 自動(dòng)拆箱則相反;

  • 數(shù)據(jù)類型:

Java中的基本數(shù)據(jù)類型: boolean, byte, char, short, int, long ,float, double
Java中對(duì)應(yīng)的包裝類型: Boolean, Byte, Character, Short, Integer, Long, Float, Double

  • 自動(dòng)裝箱拆箱是編譯器幫我們自動(dòng)轉(zhuǎn)換的,我們不需要手工調(diào)用valueOf()和intValue()方法:

自動(dòng)裝箱時(shí)調(diào)用包裝類的valueOf方法,比如Integer.valueOf(100)
自動(dòng)拆箱時(shí)調(diào)用包裝類的xxxValue方法,比如intObj.intValue()

自動(dòng)裝箱:

  1. 把一個(gè)int值賦給Integer類型:

    Integer i = 11;    // 自動(dòng)裝箱,   Integer i = Integer.valueOf(11);
    

2.集合類添加原始類型時(shí)的自動(dòng)裝箱:

  Map<Integer, Object>  map = new Map<Integer, Object>;
  map.put(1, "123");  //  自動(dòng)裝箱:  map.put(Integer.valueOf(1), "123" );
  map.put(2, "456");
  List<Integer> list = new ArrayList<>;
  list.add(2);  //  自動(dòng)裝箱: list.add(Integer.valueOf(2));
  list.add(200);

3.自動(dòng)裝箱的緩存問(wèn)題:

Integer  i11 = 3;
Integer  i12 = 3;
Integer  i21 = 300;
Integer  i22 = 300;
System.out.println(i11 == i12);  // true
System.out.println(i21 == i22);  // false

在Java里 ‘==’ 是比較對(duì)象引用(地址)的,如果兩個(gè)對(duì)象引用指向堆中的同一塊內(nèi)存就返回true,否則返回false。
根據(jù)自動(dòng)裝箱規(guī)則我們知道Integer intObj = 1 <==> Integer intObj = Integer.valueOf(1);,鬼就出在valueOf方法上,查看源碼:

public static Integer  valueOf(int value){
        assert  IntegerCache.high >= 127;
        if(i >= IntegerCache.low && i <= IntegerCache.high ){
              return IntegerCache.cache[i + (-IntegerCache.low )]
        }
        return new Integer(i);
}

如果valueOf(i)的參數(shù)i再[low,high]范圍內(nèi),就直接從緩存里面取出一個(gè)事先new好的對(duì)象返回,這里low,high默認(rèn)值分別為-128, 127,即i如果落在[-127, 128],就會(huì)直接返回緩存里面的對(duì)象。

對(duì)于Integer,我們可以通過(guò)-Djava.lang.Integer.IntegerCache.high=10000或者 -XX:AutoBoxCacheMax=2000(server模式啟動(dòng))來(lái)設(shè)置緩存的范圍。

除了Integer,其它包裝類型對(duì)緩存的使用如下:

  • Boolean就兩個(gè)緩存值TRUE,F(xiàn)ALSE
  • Byte占1個(gè)字節(jié),表示范圍是[-128,127], 因?yàn)榉秶淮?,所以全?56個(gè)值都是緩存的
  • Short, Long緩存范圍是[-128,127],而且不可更改. 也無(wú)法通過(guò)extends方式擴(kuò)展,因?yàn)檫@兩個(gè)類是final的
  • Float,Double沒(méi)有緩存,每次valueOf(1.0)都是返回一個(gè)堆中的新對(duì)象.

自動(dòng)拆箱

1.Integer和int類型進(jìn)行 == > >= < <=比較時(shí),會(huì)把Integer自動(dòng)拆箱:

Integer integer == 3;
System.out.print(integer == 3);  // true,  自動(dòng)拆箱:  integer.intValue() == ;
System.out.print(integer > 3);  //  自動(dòng)拆箱:

2.Integer和Integer進(jìn)行 > >= < <=比較時(shí),兩個(gè)都會(huì)自動(dòng)拆箱:

Integer integerOne = 3;
Integer integerTwo = 4;
System.out.print(integerOne == integerTwo);   
System.out.print(integerOne < integerTwo);   //  自動(dòng)拆箱: integerOne.intValue() < integerTwo.intValue()

3.switch會(huì)自動(dòng)拆箱,但是case后面只能是常量 (字面量或者常變量):

final int i =1;
Integer integer = 3;
switch(integer){  // 自動(dòng)拆箱
    case 0:
              break;
    case i:
              break;
    default:
             System.out.print("default !!!");   
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容