JDK的源码,看看到Integer.ValueOf(int)里面做了什么优化:

 

从源代码可以知道,ValueOf对-128~127这256个值做了缓存(IntegerCache),如果int值的范围是:-128~127,在ValueOf(int)时,他会直接返回IntegerCache的缓存给你。

 

 

结果是:

true
false

因为:java在编译的时候 Integer a = 100; 被翻译成-> Integer a = Integer.valueOf(100);,所以a和b得到都是一个Cache对象,并且是同一个!而c和d是新创建的两个不同的对象,所以c自然不等于d。

相关文章:

  • 2022-12-23
  • 2021-05-25
  • 2022-12-23
  • 2021-05-29
  • 2021-08-18
  • 2022-01-14
  • 2021-09-21
  • 2022-12-23
猜你喜欢
  • 2021-12-18
  • 2021-08-21
  • 2021-04-02
  • 2021-07-15
  • 2021-10-21
  • 2021-11-02
  • 2022-01-18
相关资源
相似解决方案