https://blog.csdn.net/magician_Code/article/details/51469101

我们先来看看下面代码的运行情况:

public static void main(String[] args)
{
// TODO Auto-generated method stub
Integer integer1;
Integer integer2; integer1 = new Integer(10);
integer2 = new Integer(10);
//第一次比较
System.out.println("第一次比较:"+(integer1==integer2)); //第二次比较
System.out.println("第二次比较:"+(integer1==10)); integer1 = 127;
integer2 = 127;
//第三次比较
System.out.println("第三次比较:"+(integer1==integer2)); integer1 = 128;
integer2 = 128;
//第四次比较
System.out.println("第四次比较:"+(integer1==integer2));
}

运行程序,结果如下:

你看出了运行结果了吗?

第一次和第二次比较就无可厚非了,第一次是直接把两个不同的对象比较,当然是false;第二次比较时,是把Integer对象和int型10进行比较,根据自动装箱、拆箱机制,这时候的比较就等价于10==10,所以是true。那么后面两个为什么会出现两种不同的结果呢?

首先我们先来看看Integer的两种定义方式:

Integer integer1 = new Integer(10);
Integer integer2 = 10;

第一种是我们常见的创建一个对象的方法,那么第二个方法呢?根据Java的自动装箱、拆箱机制,这时在Integer内部实际上是做了如下操作:

Integer integer2 = Integer.valueOf(10);

这时我们查看Integer源码中关于valueOf方法的定义:

public static Integer valueOf(int paramInt)
{
if ((paramInt >= -128) && (paramInt <= IntegerCache.high)) {
return IntegerCache.cache[(paramInt + 128)];
}
return new Integer(paramInt);
}

这里我们会留意到”IntegerCache”这个类,跟踪一下代码,发现这是Integer的一个私有内部类,声明如下:

private static class IntegerCache
{
static final int low = -128;
static final int high;
static final Integer[] cache; private IntegerCache() {} static
{
int i = 127;
String str = VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (str != null) {
try
{
int j = Integer.parseInt(str);
j = Math.max(j, 127);
i = Math.min(j, 2147483518);
}
catch (NumberFormatException localNumberFormatException) {}
}
high = i;
cache = new Integer[high - -128 + 1];
int k = -128;
for (int m = 0; m < cache.length; m++) {
cache[m] = new Integer(k++);
}
assert (high >= 127);
}
}
}

这段代码并不难读,这里类缓存了从-128到127之间的所有整型对象,意思是当使用自动装箱的方式定义一个值在-128到127的Integer对象时,我们得到的是从缓存区(IntegerCache)中返回的实例。

所以,当我们在进行上面的第三次比较时,此时的integer1和integer2是同一个对象,那么比较的结果当然是true啦。第四次是因为我们指定的值为128,>127,所以Integer内部会创建新的对象返回,所以当然不可能相等。

最后啰嗦一下,如果要进行两个Integer对象基于数值的比较时,因为Integer实现了Compaeable接口,所以直接使用compaerTo方法进行比较会比较妥当。判等的话还可以使用equals方法,于是我们把最开始的代码改成如下:

public static void main(String[] args)
{
// TODO Auto-generated method stub
Integer integer1;
Integer integer2; integer1 = new Integer(10);
integer2 = new Integer(10);
//第一次比较
//System.out.println("第一次比较:"+(integer1==integer2));
if(integer1.equals(integer2))
System.out.println("第一次比较:"+true);
else
System.out.println("第一次比较:"+false); //第二次比较
System.out.println("第二次比较:"+(integer1==10)); integer1 = 127;
integer2 = 127;
//第三次比较
//System.out.println("第三次比较:"+(integer1==integer2));
if(integer1.equals(integer2))
System.out.println("第三次比较:"+true);
else
System.out.println("第三次比较:"+false); integer1 = 128;
integer2 = 128;
//第四次比较
//System.out.println("第四次比较:"+(integer1==integer2));
if(integer1.equals(integer2))
System.out.println("第四次比较:"+true);
else
System.out.println("第四次比较:"+false);
}

Integer判等的陷阱:你知道Integer内部高速缓冲区IntegerCache吗?的更多相关文章

  1. 面试陷阱1:Integer类型的比较

    public class Test01 { public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, ...

  2. java Integer判等的大坑

    在-128 至 127 范围内的赋值,Integer 对象是在IntegerCache.cache 产生,会复用已有对象,这个区间内的 Integer 值可以直接使用==进行 判断,但是这个区间之外的 ...

  3. Microsoft Office Access

    Microsoft Office Access各版本下载地址:http://www.accessoft.com/download.html 简介 access(微软发布的关联式数据库管理系统)一般指M ...

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

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

  5. 理解Java Integer的缓存策略

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

  6. 【转载】C#之int与Java之Integer的区别

    本文涉及到一些JVM原理和Java的字节码指令,推荐感兴趣的读者阅读一本有关JVM的经典书籍<深入Java虚拟机(第2版)>,将它与我在<.NET 4.0面向对象编程漫谈>中介 ...

  7. Integer封装与拆箱

    Integer封装与拆箱 简介: 目录: Integer自动封装的陷阱 Integer自动拆箱机制 Integer自动封装的陷阱 public class IntegerDemo { public s ...

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

    本文由 ImportNew - 挖坑的张师傅 翻译自 javapapers.欢迎加入翻译小组.转载请见文末要求. 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的 ...

  9. 17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal

    01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据, ...

随机推荐

  1. Linux---设备文件名和挂载点

    分区: 1.分区: MBR   GPT 2.格式化  : 为了写入文件系统 3.设备文件名 4.什么是挂载点? 挂载点:使用已经存在的空目录作为挂载点 挂载: 必须分区: / (根分区) swap分区 ...

  2. sql语句创建数据表

    unsigned 数值类型 无符号化 AUTO_INCREMENT 自增长 PRIMARY KEY 唯一主键 COMMENT 备注信息 BIT 类型只有1和0两个值 enum 枚举数值类型 UNIQU ...

  3. Git使用(一、TortoiseGit和Gitlab在Windows下的项目库创建和上传)

    介绍使用TortoiseGit初次创建并上传到gitlab项目库,转载请注明出处. 一.需要先安装git环境,并配置Git用户名及邮箱. 二.用PuTTYgen生成公约私钥对(鼠标画画).PuTTYg ...

  4. bzoj1038(半平面交)

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #inclu ...

  5. djangoの2

    古鸽或百度的镜子:   1、E:\django下建个文件夹名为搜索引擎→PyCharm新建项目选Django→location改为E:\django\搜索引擎→More Settings的Applic ...

  6. JS获取form表单数据

    以下代码可放在一个js文件中,以便通用: //获取指定表单中指定标签对象 function getElements(formId, label) { var form = document.getEl ...

  7. 133. leetcode-Clone Graph

    拷贝图,可以一边遍历一边拷贝 DFS class Solution { public: Node* cloneGraph(Node* node) { unordered_map<int, Nod ...

  8. day18_雷神_django第一天

    # django_day01 1.http 协议 超文本传输协议,HTTP有很多应用,但最著名的是用于web浏览器和web服务器之间的双工通信. 协议概述 HTTP是一个客户端终端(用户)和服务器端( ...

  9. Web browser的发展演变

    我们每天都在使用着浏览器,每个人使用的浏览器各不一样.在这个科技飞速发展的时代,一个游览器能否站住脚跟取决于使用者的数量,看用户是否喜欢这个产品,听取用户们的意见来改善. 我们这个年龄的人最初用到的浏 ...

  10. Nuget4.0 bug一粒

    这个锅到底是nuget的还是msbuild的我也不是很确定 在使用Nuget4.0打包编译项目时 当执行到nuget pack %%~dpna.csproj -build -Prop Configur ...