java自动装箱和自动拆箱注意细节

当数值在byte范围之内时,进行自动装箱,不会新创建对象空间而是使用原来已有的空间。

Integer a = new Integer(3);
Integer b = new Integer(3);
System.out.println(a==b); //false
System.out.println(a.equals(b)); //true
System.out.println("---------------------");
Integer x = 127;
Integer y = 127;
// 在jdk1.5自动装箱时,如果数值在byte范围之内,不会新创建对象空间而是使用原来已有的空间。
System.out.println(x==y); // true
System.out.println(x.equals(y)); // true
原文地址:https://www.cnblogs.com/echoing/p/8665325.html