Java基本数据类型装箱的127临界点

 1 package wrapper.demo;
 2 
 3 public class WrapperDemo
 4 {
 5 
 6     /**
 7      * @param args
 8      */
 9     public static void main(String[] args)
10     {
11         
12         // JDK1.5以后,自动装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟新空间
13         Integer a = 127; 
14         Integer b = 127;
15         System.out.println(a == b);//true
16         System.out.println(a.equals(b));//true
17         
18         Integer c = 128; 
19         Integer d = 128;
20         System.out.println(c == d);//false  
21         System.out.println(c.equals(d));//true
22          
23         Integer x = new Integer(127);
24         Integer y = new Integer(127);
25         System.out.println(x == y); //false,因为==是比较对象地址,两个new,显然地址不一样
26         System.out.println(x.equals(y));//true,重写了equals方法,只比较数值是否相等,显然相等
27         
28     }
29 
30 }

 

posted @ 2015-03-02 18:19  90Zeng  阅读(414)  评论(0编辑  收藏  举报