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. 版本控制——TortoiseSVN (3)多版本发布

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  2. jQuery.fn的作用是什么

    jQuery.fn的作用是什么:在自定义jQuery插件中,会经常见到jQuery.fn的身影,下面就简单介绍一下它的作用到底是什么.想要认识它的本质,最好的办法直接看jQuery的源码,否则一切都是 ...

  3. CCF系列之图像旋转(201503-1)

    试题编号: 201503-1时间限制: 5.0s 内存限制: 256.0MB 问题描述 旋转是图像处理的基本操作,在这个问题中,你需要将一个图像逆时针旋转90度. 计算机中的图像表示可以用一个矩阵来表 ...

  4. MyBatis动态传入表名,字段名参数的解决办法

    原文:http://blog.csdn.net/xichenguan/article/details/50393748 要实现动态传入表名.列名,需要做如下修改 添加属性statementType=& ...

  5. mybatis-自定义typeHandler

    场景一:有个java.util.Date在存入数据库的时候自动转换为timestamp时间戳,从数据库取值的时候把时间戳自动转换为java.util.Date 表结构 CREATE TABLE `us ...

  6. python3 第十四章 - 数据类型之Dictionary(字典)

    在python中字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包括在花括号({})中 ,格式如下 ...

  7. js_10_dom表单

    事件的优先级? 先执行事件,后执行标签内置事件,如果事件返回false不执行后面的事件或标签内置事件 如何通过js提交表单? 任意标签定义onclick事件 函数中写入:document.getEle ...

  8. java1.8--1.8入门介绍

    在我之前的工作中,一直使用的是java6.所以即使现在java已经到了1.8,对于1.7增加的新特性我也基本没使用的.在整理一系列1.8的新特性的过程中,我也会添加上1.7增加的特性. 下面的整理可能 ...

  9. 自己模拟的一个简单的web服务器

    首先我为大家推荐一本书:How Tomcat Works.这本书讲的很详细的,虽然实际开发中我们并不会自己去写一个tomcat,但是对于了解Tomcat是如何工作的还是很有必要的. Servlet容器 ...

  10. Execption:the database returned no natively generated identity value

    org.hibernate.HibernateException: The database returned no natively generated identity value at org. ...