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.基本类型包装类的产生 在实际程序使用中,程序界面上用户输入的数据都是以字符串类型进行存储的.而程序开发中,我们需要把字符串数据, ...
随机推荐
- 【js jQuery】map集合 循环迭代取值---以及 map、json对象、list、array循环迭代的方法和区别
后台给前台传来一个map @ResponseBody @RequestMapping(value = "getSys") public Map<Long,String> ...
- windows 10 安装可视化mycat
前提: 1.安装配置好JDK环境 2.安装配置好mysql 3.安装配置好Navicat 一.下载mycat git:https://github.com/MyCATApache/Mycat-down ...
- 代码文档生成工具Doxygen的使用备忘
Doxygen备忘 下载与安装 一般步骤 生成配置文件Doxyfile: doxygen (-s) -g 建立目录结构, 比如Doxyfile文件\doc文件夹(输出)\src文件夹(放代码) -&g ...
- Nginx 教程(1):基本概念
简介 嗨!分享就是关心!所以,我们愿意再跟你分享一点点知识.我们准备了这个划分为三节的<Nginx教程>.如果你对 Nginx 已经有所了解,或者你希望了解更多,这个教程将会对你非常有帮助 ...
- impala教学视频
https://www.iqiyi.com/playlist394935102.html
- 简单css实现input提示交互动画效果
通过基础CSS实现输入提示交互动画效果,并兼容各浏览器! 1.效果展示 2.css代码 h4 { margin: 30px 0; } input { margin:; font-size: 16px; ...
- 我所理解Java集合框架的部分的使用(Collection和Map)
所谓集合,就是和数组类似——一组数据.java中提供了一些处理集合数据的类和接口,以供我们使用. 由于数组的长度固定,处理不定数量的数据比较麻烦,于是就有了集合. 以下是java集合框架(短虚线表示接 ...
- Azure Active Directory document ---reading notes
微软利用本地活动目录 Windows Server Active Directory 进行身份认证管理方面具有丰富的经验,现在这一优势已延伸基于云平台的Azure Active Directory.可 ...
- c# 使用资源文件
1.新建项目 2.新建资源文件 3. 代码中使用嵌入资源 using System;using System.Collections.Generic;using System.Text;using S ...
- C#通过COM组件操作IE浏览器(一):打开浏览器跳转到指定网站
简介Internet Explorer对象模型 1.属性 属性 类型 描述 Application Object 返回对Internet Explorer对象的引用. Busy Boolean 返回一 ...