我是靠谱客的博主 美好薯片,这篇文章主要介绍Integer类有一个缓存,现在分享给大家,希望可以做个参考。

Integer类有一个缓存,会缓存介于-128~127之间的整数

public class Test {

public static void main(String[] args) { 
 Integer i1 = 127; 
  Integer i2 = 127; 
System.err.println(i1 == i2); 
   i1 = 128; 
   i2 = 128; 
System.err.println(i1 == i2);
System.err.println(i1.hashCode() == i2.hashCode()); 

}

true
false
true

Integer.valueOf(int i) 优先取缓存的Integer,而不采用构造方法。如手动采用构造方法,则不采用缓存。

,会缓存介于-128~127之间的整数。

.当数值不在-128~127时,无论通过哪种方式,即使两个对象的值相等,通过“==”比较,其结果为false;

Integer重写了hashCode方法,返回值是value,即Integer对象 的数值。

 

 

最后

以上就是美好薯片最近收集整理的关于Integer类有一个缓存的全部内容,更多相关Integer类有一个缓存内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(136)

评论列表共有 0 条评论

立即
投稿
返回
顶部