Java的自动装箱和拆箱

 1 public class BoxingDemo {
 2 
 3     public static void main(String[] args) {
 4         
 5          /**
 6           * 自动装箱示例:
 7           * 基本类型int是不能直接赋值给其包装类对象Integer的,但是这里这条语句可以编译通过
 8           * 因为自动装箱原理隐式包含了下面2条语句:
 9           * Integer temp = new Integer(1);
10           * int1 = temp;
11           * */
12         Integer int1 = 1;
13         
14         /**
15           * 自动拆箱示例:
16           * Integer对象复制给基本类型
17           * 自动拆箱原理隐式包含了下面2条语句:
18           * int temp = int1.intValue();
19           * int2 = temp;
20           * */
21         int int2 = int1;
22         
23         System.out.println(int2);
24         
25         //输出结果为1,Java5以前的版本,需要手动完成装箱和拆箱工作
26 
27     }
28 
29 }

 

posted on 2014-08-21 10:13  不会熬夜的程序员  阅读(504)  评论(0编辑  收藏  举报

导航