public class Demo1 {
public static void main(String[] args) {
Integer a = new Integer(128);
Integer b = new Integer(128);
System.out.println("a == b --------->" + (a == b));// false
System.out.println("a.equals(b)-------->" + (a.equals(b)));// true
System.out.println();
/*
* 在Integer的equals方法中,比较的是数值,而不是对象
*/
Integer x = 128;
Integer y = 128;
System.out.println("x == y------------>" + (x == y));// false
System.out.println("x.equals(y)--------->" + (x.equals(y)));// true
System.out.println();
Integer p = 127;
Integer q = 127;
System.out.println("p == q------------>" + (p == q));// true
System.out.println("p.equals(q)--------->" + (p.equals(q)));// true
System.out.println();
/*
* JDk1.5以后,自动装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间
*/
}
}
自动装箱和自动拆箱的细节
最新推荐文章于 2024-05-25 09:19:17 发布