自动装箱和自动拆箱的细节

来源:互联网 发布:php linux 编辑:程序博客网 时间:2024/06/04 17:56
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));// falseSystem.out.println("a.equals(b)-------->" + (a.equals(b)));// trueSystem.out.println();/* * 在Integer的equals方法中,比较的是数值,而不是对象 */Integer x = 128;Integer y = 128;System.out.println("x == y------------>" + (x == y));// falseSystem.out.println("x.equals(y)--------->" + (x.equals(y)));// trueSystem.out.println();Integer p = 127;Integer q = 127;System.out.println("p == q------------>" + (p == q));// trueSystem.out.println("p.equals(q)--------->" + (p.equals(q)));// trueSystem.out.println();/* * JDk1.5以后,自动装箱,如果装箱的是一个字节,那么该数据会被共享,不会重新开辟空间 */}}

原创粉丝点击