1. Integer a = 100,b = 100,c=130,d=130;
  2. System.out.println(a == b);
  3. System.out.println(c==d);
  4.  

 打印结果为:

第一个是true;

第二个是flase;

原因:a,b,c,d四个变量都是integer对象的引用,所以==比较的不是值,而是引用,如果整型字面量的值在-128

到127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,否则就要new 新的Integer对象!

 

注意到:如果类型声明为int而不是Integer的时候 那么打印的结果都为true

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2022-12-23
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
猜你喜欢
  • 2021-06-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-23
  • 2021-12-09
  • 2021-12-12
相关资源
相似解决方案