深入了解java自动装箱和拆箱
今天看《深入理解JAVA虚拟机》,发现自己对自动装箱和拆箱有一些误解,在此写下来: 问题1、 Integer a = 3; Integer b = 3; 请问 a==b 返回 true 还是 false ? 开始毫不犹豫选择了false ,明显 a和b不是同一个对象。结果亲自运行这段代码发现返回true。 问题2、 Integer c = 333; Integer d= 333; 请问 c==d 返回 true 还是 false ? 亲自试验之后问题2返回false。c.equals(d) 返回true。 不解了吧?如果问题1返回true,问题2为什么返回false呢? 首先说下自动装箱:Integer自动装箱时是调用valueOf(int i)来完成自动装箱的,再来看Integer的valueOf(int i)的源码: public static Integer valueOf(int i) { final int offset = 128; if (i >= -128 && i <= 127) { // must cache return IntegerCache.cache[i + offset]; } return new Integer(i); } 原来-128-127之间的整数返回的是缓存,大于这个区间的才返回新的Integer对象。