自动拆箱和装箱

自动拆箱和装箱

    /**
     * 整数类型的自动拆箱和装箱。
     */
    public static void intAutoBox() {
        int i = 100;
        // 可以将基本数字类型赋给数字对象。
        // 在J2SE5.0之前,必须用iObj = new Integer(200);
        Integer iObj = 200;// 将200装箱
        System.out.println("开始时:i = " + i + "; iObj = " + iObj);
        Integer tempObj = iObj;
        iObj = i;
        // 将数字对象赋给基本数字类型
        // 在J2SE5.0之前,必须用i = tempObj.intValue();
        i = tempObj;// 将对象拆箱
        System.out.println("将i与iObj的值互换后:" "i = " + i + "; iObj = " + iObj);
        // 在表达式内可以自动拆箱和装箱
        iObj += i + tempObj;
        i *= iObj + tempObj;
 
.......
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章