亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    深入理解java中的自動裝箱與拆箱

    深入理解java中的自動裝箱與拆箱

    一、什么是裝箱,什么是拆箱

    裝箱:把基本數(shù)據(jù)類型轉換為包裝類。

    拆箱:把包裝類轉換為基本數(shù)據(jù)類型。

    基本數(shù)據(jù)類型所對應的包裝類:

    int(幾個字節(jié)4)- Integer

    byte(1)- Byte

    short(2)- Short

    long(8)- Long

    float(4)- Float

    double(8)- Double

    char(2)- Character

    boolean(未定義)- Boolean

    免費在線視頻學習教程推薦:java視頻教程

    二、先來看看手動裝箱和手動拆箱

    例子:拿int和Integer舉例

    Integer i1=Integer.valueOf(3); int i2=i1.intValue();

    手動裝箱是通過valueOf完成的,大家都知道 = 右邊值賦給左邊,3是一個int類型的,賦給左邊就變成了Integer包裝類。

    手動拆箱是通過intValue()完成的,通過代碼可以看到 i1 從Integer變成了int

    三、手動看完了,來看自動的

    為了減輕技術人員的工作,java從jdk1.5之后變?yōu)榱俗詣友b箱與拆箱,還拿上面那個舉例:

    手動:

    Integer i1=Integer.valueOf(3); int i2=i1.intValue();

    自動

    Integer i1=3; int i2=i1;

    這是已經(jīng)默認自動裝好和拆好了。

    四、從幾道題目中加深對自動裝箱和拆箱的理解

    (1)

    Integer a = 100; int b = 100; System.out.println(a==b);結果為 true

    原因:a 會自動拆箱和 b 進行比較,所以為 true

    (2)

    Integer a = 100; Integer b = 100; System.out.println(a==b);//結果為true Integer a = 200; Integer b = 200; System.out.println(a==b);//結果為false

    這就發(fā)生一個有意思的事了,為什么兩個變量一樣的,只有值不一樣的一個是true,一個是false。

    原因:這種情況就要說一下 == 這個比較符號了,== 比較的內(nèi)存地址,也就是new 出來的對象的內(nèi)存地址,看到這你們可能會問這好像沒有new啊,但其實Integer a=200; 200前面是默認有 new Integer的,所用內(nèi)存地址不一樣 == 比較的就是 false了,但100為什么是true呢?這是因為 java中的常量池 我們可以點開 Integer的源碼看看。

    private static class IntegerCache {     static final int low = -128;     static final int high;     static final Integer cache[];      static {         // high value may be configured by property         int h = 127;         String integerCacheHighPropValue =             sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");         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 之間的進行比較時,不會new 對象,而是直接到常量池中獲取,所以100是true,200超過了這個范圍然后進行了 new 的操作,所以內(nèi)存地址是不同的。

    (3)

    Integer a = new Integer(100); Integer b = 100; System.out.println(a==b); //結果為false

    這跟上面那個100的差不多啊,從常量池中拿,為什么是false呢?

    原因:new Integer(100)的原因,100雖然可以在常量池中拿,但架不住你直接給new 了一個對象啊,所用這倆內(nèi)存地址是不同的。

    (4)

    Integer a = 100; Integer b= 100; System.out.println(a == b); //結果true a = 200; b = 200; System.out.println(c == d); //結果為false

    原因:= 號 右邊值賦給左邊a,b已經(jīng)是包裝類了,200不在常量池中,把int 類型200 賦給包裝類,自動裝箱又因為不在常量池中所以默認 new了對象,所以結果為false。

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號