public class Test01 {
public static void main(String[] args) {
Integer f1 = 100, f2 = 100, f3 = 150, f4 = 150; System.out.println(f1 == f2);  //true
System.out.println(f3 == f4);  //false
}
}

如果不明就里很容易认为两个输出要么都是true要么都是false。首先需要注意的是f1、f2、f3、f4四个变量都是Integer对象引用,所以下面的==运算比较的不是值而是引用。装箱的本质是什么呢?当我们给一个Integer对象赋一个int值的时候,会调用Integer类的静态方法valueOf,如果看看valueOf的源代码就知道发生了什么。

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

IntegerCacheInteger的内部类,其代码如下所示:

/**
* 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() {}
}

所以,如果整型字面量的值在-128127之间,那么不会new新的Integer对象,而是直接引用常量池中的Integer对象,所以上面的面试题中f1==f2的结果是true,而f3==f4的结果是false。

面试陷阱1:Integer类型的比较的更多相关文章

  1. 面试官:如何在Integer类型的ArrayList中同时添加String、Character、Boolean等类型的数据? | Java反射高级应用

    原文链接:原文来自公众号:C you again,欢迎关注! 1.问题描述     "如何在Integer类型的ArrayList中同时添加String.Character.Boolean等 ...

  2. JAVA包装类解析和面试陷阱分析

    包装类 什么是包装类 虽然 Java 语言是典型的面向对象编程语言,但其中的八种基本数据类型并不支持面向对象编程,基本类型的数据不具备“对象”的特性——不携带属性.没有方法可调用. 沿用它们只是为了迎 ...

  3. java中两个Integer类型的值相比较的问题

    今天在做一个算法时,由于为了和其他人保持接口的数据类型一致,就把之前的int换为Integer,前几天测了几组数据,和之前的结果一样,但是今天在测其它数据 的时候,突然出现了一个奇怪的bug,由于之前 ...

  4. Hibernate 分页时 Long 无法转化成Integer类型 异常

    转自:http://loquat.iteye.com/blog/818547 报错:java.lang.Long cannot be cast to java.lang.Integer   Long ...

  5. 转:JAVA里面的int类型 和Integer类型,有什么不一样

    JAVA里面的int类型 和Integer类型,有什么不一样 原文链接:http://blog.csdn.net/wuxinliulei/article/details/11099565 java.l ...

  6. mybatis中使用Integer类型的参数<if>判断问题

    mybatis对传入参数进行判断时,会使用if标签, 一般是判断不为null和'', 如下: <if test="name != null and 那么 != ''"> ...

  7. 我今天遇到的条件语句Integer类型的

    两个Integer类型的值进行比较时,应该用equals进行判断,用"=="判断是错误的,后来想了一下就明白了,Integer毕竟是对象, 而不是int基本数据类型,可以直接比较, ...

  8. 维度属性的KeyColumns如果是Integer类型,那么维度表中该列的值不能有为null的

    如果维度属性的 KeyColumns的DataType设置为了Integer类型,那么要注意该维度属性列在数据库中不能有为null的值. 例如下图中我们有维度DIM_Vehcile,其中有个维度属性叫 ...

  9. 【mybatis】mybatis查询发生条件传入值但是查询并没有这个条件的查询,Integer类型查询条件需要注意事项

    有下面这样一个查询: 下面标紫色的查询条件,type的类型为Integer <select id="findDealerInfo" parameterType="c ...

  10. ibatis中integer类型

    假如java代码中设置的返回类型是integer类型,ibatis实际上返回的是BigDecimal,也就是说  ibatis转换成integer默认是bigdecimal类型的

随机推荐

  1. bootstrap 响应式图片自适应图片大小

    <img src="..." class="img-responsive center-block" > 或者 $(window).load(fun ...

  2. ASP.NET导出word实例

    ASP.NET导出word实例 最近遇到一个题目就是如何在asp.net中将数据导出到word中,由于数据是动态的,所以需要在后台拼出想要的的格式,翻遍了网页找出了一个比较满意的代码,感谢那位高手.代 ...

  3. Linux - ubuntu vMwareTools安装

    ubuntu vMwareTools安装 不安装很麻烦,虚拟机中的内容,包括文件.无法复制到pc端.同样的pc端的内容也无法复制到虚拟机中. 1.点击虚拟机,选择安装VMwareTools 这个时候就 ...

  4. Source Insight、Xshell(putty)、Xftp

    body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...

  5. bxslider使用教程

    bxSlider下载+参数说明 "bxSlider"就是一款响应式的幻灯片js插件 bxSlider特性 充分响应各种设备,适应各种屏幕: 支持多种滑动模式,水平.垂直以及淡入淡出 ...

  6. 译-BSA NSH Command介绍

    BSA NSH Command全称BMC BladeLogic Network Shell Command,是基于ZSH的shell. 1 说明 NSH命令行(全称Network  Shell,又称为 ...

  7. 流API--流的迭代

    虽然流不是数据存储对象,但是仍然可以使用迭代器来遍历其元素,就如同使用迭代器遍历集合中的元素一样. 流API支持2类迭代器,一类是传统的Iterator,另一类是JDK8新增的Spliterator. ...

  8. grails项目中(DB的相关操作)

    grails项目中(DB的相关操作) save:保存Domain对象的数据到对应的库表中(可能是insert也可能是update) findBy: 动态方法,查找并返回第一条记录,方法名可以变化 eg ...

  9. 各模拟器adb连接端口

    如果执行自动化测试,在没有真机的情况下,我们唯一的选择便是模拟器.目前市面上有很多模拟器,他们使用adb连接时都会有不同的默认接口,当adb无法自动连接模拟器时,手动使用ip+端口连接是很好的选择.下 ...

  10. KBEngine游戏服务器(一)——引擎环境配置

    系统:Win10 版本:Visual Studio 2013(也就是vs120) kbengine:v1.0.0 MySQL:5.7 MySQL Workbench :6.3 一.下载kbengine ...