关于自动装箱和自动拆箱

1 Integer a = new Integer(1);
2 Integer b = Integer.valueOf(1);
3 Integer c = inc(0);
4 Integer d = 1;
1 public static Integer inc(Integer x) {
2         // return这里的相加应该是先拆箱然后相加吧?(这里不确定)
3         // 相加为1后又装箱到Integer里面 而装箱默认调用的是valueOf所以b和c == 为true
4         return x + 1;
5     }

 

猜想一下a b c d是否都相等呢?

 

 

结果是:a和bcd都不相等  bcd相等

因为a是直接new的对象所以值是都相等的,但是用==比较的是对象的引用地址,所以不相等

第二三四行因为是取的缓存中的数

第三四行其实都会默认调用Integer.valueOf()

以上仅仅个人理解,有疑问或者错误,请评论

posted @ 2017-05-04 14:28  风雨的叶  阅读(127)  评论(0编辑  收藏  举报