美文网首页
自动装箱与拆箱

自动装箱与拆箱

作者: hainingwyx | 来源:发表于2017-03-08 14:14 被阅读11次

    写在之前

    以下是《Java8编程入门官方教程》中的一些知识,如有错误,烦请指正。涉及的程序如需下载请移步http://down1.tupwk.com.cn/qhwkdownpage/978-7-302-38738-1.zip

    自动装箱

    简化了在基本类型和对象之间转换所需的代码。

    类型封装器

    主要有:Double、Float、Long、Integer、Short、Byte、Character、Boolean,它们包含在java.lang中。把值封装在对象中的过程称为装箱。

    Integer iOb = new Integer(100); //手动装箱
    

    从类型封装器中提取值的过程称为拆箱。

    int i = iOb.intValue();//手动拆箱
    

    自动装箱是指在需要某种类型的数据对象时,把该基本类型自动装箱到其等效的类型封装器的过程。不需要显式地构造对象。

    自动拆箱是指当需要某个装箱对象的值时,从类型封装其实把装箱对象的值自动地提取出来的过程。

    Integer iOb =100; //自动装箱
    int i = iOb;    //auto-unbox
    

    除了赋值的情况,自动装箱在基本类型必须转化为对象时会自动发生,自动拆箱在对象必须转化为基本类型时会自动发生。

    class AutoBox2 {  
      // 方法接收一个 Integer. 
      static void m(Integer v) {  
        System.out.println("m() received " + v); 
      }   
      
      //返回一个int. 
      static int m2() { 
        return 10; 
      } 
     
      // 返回一个 Integer. 
      static Integer m3() { 
        return 99; // autoboxing 99 into an Integer. 
      } 
     
      public static void main(String args[]) { 
     
        // Pass an int to m().  Because m() has an Integer 
        // parameter, the int value passed is automatically boxed. 
        m(199); 
     
        // Here, iOb recieves the int value returned by m2(). 
        // This value is automatically boxed so that it can be 
        // assigned to iOb. 
        Integer iOb = m2();   
        System.out.println("Return value from m2() is " + iOb);  
     
        // Next, m3() is called. It returns an Integer value 
        // which is auto-unboxed into an int. 
        int i = m3(); 
        System.out.println("Return value from m3() is " + i);  
     
        // Next, Math.sqrt() is called with iOb as an argument. 
        // In this case, iOb is auto-unboxed and its value promoted to 
        // double, which is the type needed by sqrt(). 
        iOb = 100; 
        System.out.println("Square root of iOb is " + Math.sqrt(iOb)); 
      }  
    }
    

    下面看一下表达式之中,数值对象被自动拆箱,表达式结果必要时重新装箱的情况。

    class AutoBox3 { 
      public static void main(String args[]) { 
         
        Integer iOb, iOb2; 
        int i; 
     
        iOb = 99; 
        System.out.println("Original value of iOb: " + iOb); 
     
        // The following automatically unboxes iOb, 
        // performs the increment, and then reboxes 
        // the result back into iOb. 
        ++iOb; 
        System.out.println("After ++iOb: " + iOb); 
     
        // Here, iOb is unboxed, its value is increased by 10 
        // and the result is boxed and stored back in iOb. 
        iOb += 10;  
        System.out.println("After iOb += 10: " + iOb); 
     
        // Here, iOb is unboxed, the expression is  
        // evaluated, and the result is reboxed and 
        // assigned to iOb2. 
        iOb2 = iOb + (iOb / 3); 
        System.out.println("iOb2 after expression: " + iOb2); 
     
        // The same expression is evaluated, but the 
        // result is not reboxed. 
        i = iOb + (iOb / 3); 
        System.out.println("i after expression: " + i); 
      }  
    }
    

    注意:使用封装类型做计算,因为涉及到自动装箱和拆箱,比使用基本类型,代码的效率要低很多。因为每次装箱和拆箱都增加了基本类型没有的开销。

    相关文章

      网友评论

          本文标题:自动装箱与拆箱

          本文链接:https://www.haomeiwen.com/subject/iqubgttx.html