先看代码实例现象:

问题:为什么都是比较数值,第一个为true,第二个确为false呢?

查找源码(java.lang.Integer),看到如下代码:

/**
* Cache to support the object identity semantics of autoboxing for values between
* -128 and 127 (inclusive) as required by JLS.
*
* The cache is initialized on first usage. The size of the cache
* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
* During VM initialization, java.lang.Integer.IntegerCache.high property
* may be set and saved in the private system properties in the
* sun.misc.VM class.
*/ private static class IntegerCache {
static final int low = -128;
static final int high;
static final Integer cache[]; static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
try {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
} catch( NumberFormatException nfe) {
// If the property cannot be parsed into an int, ignore it.
}
}
high = h; cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++); // range [-128, 127] must be interned (JLS7 5.1.7)
assert IntegerCache.high >= 127;
} private IntegerCache() {}
}

原来是因为Integer类型使用了缓存机制,即默认在JVM启动的时候设定了[-127~128]范围内的int包装类,这样在实际使用并在范围内的时候,直接从缓存中取实例,不用再new了。

这样做的好处是:提升JVM的性能。

坏处是:用==来比较Integer类型的时候,可能出现问题。

解决方案:

  • 设定JVM启动参数-XX:AutoBoxCacheMax=<size>,改编默认的缓存最大(不推荐)
  • 不要用==比较大小,用equals比较(推荐)

同样使用缓存的还有ShortCache, LongCache

IntegerCache类的更多相关文章

  1. Integer 中的缓存类IntegerCache

    2014年去某公司笔试的时候遇到这么一道题: public class Test { public static void main(String[] args) { Integer int1 = I ...

  2. java基础知识回顾之---java String final类 容易混淆的java String常量池内存分析

    /** *   栈(Stack) :存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(字符串常量对象存放  在常量池中). 堆(heap):存 ...

  3. 动手动脑(&课后实验):类和对象

    1. 以下代码为何无法通过编译?哪儿出错了? 如果类提供了一个自定义的构造方法,将导致系统不再提供默认构造方法.而此时程序中已提供了一个有一个参数的构造函数,而定义对象时却没有参数,所以程序会报错. ...

  4. Integer IntegerCache源码

    先看一段测试结果: /*public static void main(String[] args) { Integer a = 128, b = 128; Integer c = 127, d = ...

  5. AJPFX总结java开发常用类(包装,数字处理集合等)(一)

    一:首谈java中的包装类 Java为基本类型提供包装类,这使得任何接受对象的操作也可以用来操作基本类型,直接将简单类型的变量表示为一个类,在执行变量类型的相互转换时,我们会大量使用这些包装类.jav ...

  6. Java Interger类,两对整数明明完全一样,为何一个输出true,一个输出false

    package text; public class MethodOverload { public static void main(String[] args) { Integer i1=100; ...

  7. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  8. 【转】理解Java Integer的缓存策略

    本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性.首先看一个使用 Integer 的示例代码,展示了 Integer 的缓存行为 ...

  9. 理解Java Integer的缓存策略

    转载自http://www.importnew.com/18884.html 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性. ...

随机推荐

  1. Python全栈开发day8

    一.python生成/迭代器 yiled生成数据 python迭代器, 访问数据(通过next一次一次去取) 二.反射 通过字符串的形式,导入模块 通过字符串的形式,到模块中,寻找指定的函数,并执行 ...

  2. love easily fade

      Ubuntu addict and loving it , the answerer 's intro attracted me.When I first read a book on linux ...

  3. 构建Maven聚合工程

    最近,准备构建一个Maven的项目,基于模块化的结构思想,决定使用Maven的聚合工程进行构建. 环境准备: (1)eclipse 这里推荐使用spring封装的eclipse(即STS) ,STS中 ...

  4. Android_Activity生命周期

    通过前面一段时间的学习,我们很清楚我们的一系列操作都离不开的一个东西,就是我们的activity .接下来我们对 Activity 进行系统的总结. Activity 的四种基本状态 1.运行态(Ru ...

  5. Android Studio新建一个HelloWorld 程序(App)

    Android Studio新建一个HelloWorld程序(App) 新建 或者直接启动程序(注:如果已有程序,此方法会直接打开最近一次关闭从程序) 更改App名 选择App运行平台 选择模板 更改 ...

  6. Linux secure boot(安全启动)时添加Nvidia显卡驱动

    开启Secure boot情况下,在Fedora 21下安装Nvidia 显卡驱动的方法. Nvidia显卡驱动可以从官网上下载最新版>> 点击进入 下载后添加可执行权限: #chmod ...

  7. [C语言]关于struct和typedef struct

    在C中定义一个结构体类型要用typedef: *************************************************************************** t ...

  8. Tomcat服务启动成功,但访问index.jsp出错 (jspInit)

    本文引用自 --> http://zhouhaitao.iteye.com/blog/1164736 Tomcat服务启动成功,但访问index.jsp出错 环境:Tomcat6 + jdk6 ...

  9. Light OJ 1030 - Discovering Gold(概率dp)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1030 题目大意:有一个很长的洞穴, 可以看做是1-n的格子.你的起始位置在1的 ...

  10. mysql中的SUBSTRING_INDEX

    SUBSTRING_INDEX(str,delim,count) Returns the substring from string str before count occurrences of t ...