久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

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

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

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

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

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

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

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

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

      byte(1)- Byte

      short(2)- Short

      long(8)- Long

      float(4)- Float

      double(8)- Double

      char(2)- Character

      boolean(未定義)- Boolean

      免費(fèi)在線視頻學(xué)習(xí)教程推薦:java視頻教程

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

      例子:拿int和Integer舉例

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

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

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

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

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

      手動:

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

      自動

      Integer i1=3; int i2=i1;

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

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

      (1)

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

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

      (2)

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

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

      原因:這種情況就要說一下 == 這個比較符號了,== 比較的內(nèi)存地址,也就是new 出來的對象的內(nèi)存地址,看到這你們可能會問這好像沒有new啊,但其實(shí)Integer a=200; 200前面是默認(rèn)有 new Integer的,所用內(nèi)存地址不一樣 == 比較的就是 false了,但100為什么是true呢?這是因?yàn)?java中的常量池 我們可以點(diǎn)開 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 之間的進(jìn)行比較時,不會new 對象,而是直接到常量池中獲取,所以100是true,200超過了這個范圍然后進(jìn)行了 new 的操作,所以內(nèi)存地址是不同的。

      (3)

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

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

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

      (4)

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

      原因:= 號 右邊值賦給左邊a,b已經(jīng)是包裝類了,200不在常量池中,把int 類型200 賦給包裝類,自動裝箱又因?yàn)椴辉诔A砍刂兴阅J(rèn) new了對象,所以結(jié)果為false。

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