Integer判等的陷阱:你知道Integer内部高速缓冲区IntegerCache吗?
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:Integer类型的比较
public class Test01 { public static void main(String[] args) { Integer f1 = 100, f2 = 100, f3 = 150, ...
- java Integer判等的大坑
在-128 至 127 范围内的赋值,Integer 对象是在IntegerCache.cache 产生,会复用已有对象,这个区间内的 Integer 值可以直接使用==进行 判断,但是这个区间之外的 ...
- Microsoft Office Access
Microsoft Office Access各版本下载地址:http://www.accessoft.com/download.html 简介 access(微软发布的关联式数据库管理系统)一般指M ...
- 【转】理解Java Integer的缓存策略
本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性.首先看一个使用 Integer 的示例代码,展示了 Integer 的缓存行为 ...
- 理解Java Integer的缓存策略
转载自http://www.importnew.com/18884.html 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的一个有助于节省内存.提高性能的特性. ...
- 【转载】C#之int与Java之Integer的区别
本文涉及到一些JVM原理和Java的字节码指令,推荐感兴趣的读者阅读一本有关JVM的经典书籍<深入Java虚拟机(第2版)>,将它与我在<.NET 4.0面向对象编程漫谈>中介 ...
- Integer封装与拆箱
Integer封装与拆箱 简介: 目录: Integer自动封装的陷阱 Integer自动拆箱机制 Integer自动封装的陷阱 public class IntegerDemo { public s ...
- 理解Java Integer的缓存策略【转】
本文由 ImportNew - 挖坑的张师傅 翻译自 javapapers.欢迎加入翻译小组.转载请见文末要求. 本文将介绍 Java 中 Integer 缓存的相关知识.这是 Java 5 中引入的 ...
- 17_java之Integer|System|Arrays|Math|BigInteger|BigDecimal
01基本数据类型对象包装类概述 *A:基本数据类型对象包装类概述 *a.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据, ...
随机推荐
- Web Service CXF的工作流程
我们一起走进系统的内部,跟随每一个调用,去透视系统的每一个层面. 一.我们定义整个目录都在CXFServlet的监控之下 <servlet> <servlet-name>CXF ...
- 京东Alpha平台开发笔记系列(三)
摘要:通过前面两篇文章的讲述,大致了解了JdAlpha平台前端开发的主要流程.接下来本篇文章主要讲述后台服务器端开发的主要流程.这里会涉及到后台服务器的搭建的内容,本篇文章就不以赘述,如需了解请读下面 ...
- 利用python list 完成最简单的DB连接池
先来看查看效果: 在代码连接数据库后,并且执行三条sql后,将mysql直接重启掉,故我们的连接池连接均是不ok的,所以,它会全部删除再抓新的连接下来,重启mysql命令: 关于python代码: # ...
- vue组件自定义属性命名
今天自己写vue组件demo的时候发现一个有趣的问题:vue组件自定义属性命名不支持用驼峰命名! 上面图示为正常情况下的自定义属性,没有任何问题. 但是一旦出现自定义属性中包含了大写字母,则如下图所示 ...
- OpenCV Mat格式存储YUV图像
YUV图像用的比较多,而且YUV图像的格式众多(YUV格式可以参考YUV pixel formats),如何用OpenCV的Mat类型来存储YUV图像也是经常遇到的问题. 对于YUV444图像来说,就 ...
- 查看 Oracle 数据库锁和解锁的方法
-- 查看数据库锁明细 select a.sid, b.serial#, b.username, b.osuser, b.machine, b.program, c.object_name,d.spi ...
- 记录自己的 django管理 开发环境 和 生产环境 配置过程
背景:自己的博客部署到服务器了,可每次上传服务器都要把配置重新该,包括数据库链接也得改,于是就需要管理开发环境和生产环境配置. 1, 这是目录结构,在blog下新建一个settings包,里面新建有c ...
- 腾讯开源 MMKV — 基于mmap的高性能通用key-value组件
一.介绍 MMKV 是基于 mmap 内存映射的 key-value 组件,底层序列化/反序列化使用 protobuf 实现,性能高,稳定性强.从 2015 年中至今,在 iOS 微信上使用已有近 3 ...
- 程序员工作 996 生病 ICU ?
阅读本文大概需要 2 分钟. 说实话,一般平时这个点我已经睡着了,今天准备好的文章也会准时在凌晨推送给大家.睡前看篇关于强制 996 加班的消息,里面有句口号还挺溜,上班996,下班ICU,为此还特意 ...
- build.gradle文件的注释
Gradle是一种依赖管理工具,基于Froovy语言,面向Java应用为主,它抛弃了基于xml的各种繁琐配置,取而代之的是一种基于Groovy的内部领域特定(DSL)语言. apply plugin: ...