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()

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

相关文章:

  • 2019-03-04
  • 2022-01-09
  • 2022-12-23
  • 2021-05-17
  • 2021-05-11
  • 2021-07-19
  • 2021-07-24
  • 2021-07-14
猜你喜欢
  • 2021-11-08
  • 2021-06-23
  • 2022-12-23
  • 2021-09-19
  • 2022-12-23
相关资源
相似解决方案