1.问题提出

  今天在和同事讨论问题的时候,无意间谈到了Integer对象的比较,先看下代码:

package test;

public class IntegerEqual {

	/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub Integer a = 1;
Integer b = 1;
Integer c = 2000;
Integer d = 2000; System.out.println(a==b);
System.out.println(c==d); } }

  相信很多人一看到这段代码,就会感觉输出的结果是true,true;实际上这段代码的输出结果是true,false;这是为什么呢?同样的对象为什么比较的结果不一样呢?

2.结果分析

  先来分析下Integer a=1;的实现方式,1是数字是怎么放到Integer这个对象中去的;我们知道这是JDK5新增的语法自动装箱和拆箱功能实现的,可是具体的这个装箱功能室如何实现的呢?我们先来看下Integer.valueOf()这个方法,因为这是Integer默认的装箱的实现方式:

 /**
* Returns a <tt>Integer</tt> instance representing the specified
* <tt>int</tt> value.
* If a new <tt>Integer</tt> instance is not required, this method
* should generally be used in preference to the constructor
* {@link #Integer(int)}, as this method is likely to yield
* significantly better space and time performance by caching
* frequently requested values.
*
* @param i an <code>int</code> value.
* @return a <tt>Integer</tt> instance representing <tt>i</tt>.
* @since 1.5
*/
public static Integer valueOf(int i) {
if(i >= -128 && i <= IntegerCache.high)
return IntegerCache.cache[i + 128];
else
return new Integer(i);
}

  通过注释也可以发现,这是JDK5的时候新增的方法,先来简单分析下这个方法具体做了什么事情;原来她内部维护了一个缓存池,它总是Integer缓存池中获取Integer对象,超出了其缓存池缓存池(-128到127),它才new新的Integer对象。只要在这个范围内,都是直接取出对象而不是创建,所以值总是相等的,但是如果超过了这个范围,那么它就会穿件新的对象(-2147483648到-128和127到2147483647)(Integer.MAX_VALUE:2147483647,Integer.MIN_VALUE:-2147483648),一旦是创建的对象,那么比较的是对象自然是不相等,即使值是相等的。

3.其他

   public boolean equals(Object obj) {
if (obj instanceof Integer) {
return value == ((Integer)obj).intValue();
}
return false;
}

  因为这个方法取得是具体的值,所以不管是多少,具体的结果总是相等的。

Java 之Integer相等比较的更多相关文章

  1. java.lang.IllegalArgumentException: Result Maps collection does not contain value for java.lang.Integer

    今天做springmvc+mybatis+spring的项目的时候发现了一个异常.如下: org.apache.ibatis.builder.IncompleteElementException: C ...

  2. Result Maps collection does not contain value for java.lang.Integer异常处理

    使用Mybatis的时候出现这个问题是因为配置文件的问题造成的,mybatis需要写大量的配置文件, 尽管有mybatis-generator,但是里面的内容有很多还是要自己去写的,在这过程中难免会出 ...

  3. org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [41] did not match expected type [java.lang.Integer (n/a)];

    题记:以前记录过一些自己遇到的BUG,这个行为,让我一看报错的提示信息就能定位到问题的所在,后来记得比较多了,好多是重复性的再加上比较忙就没有详细的记录了,今天的工作量比较小,就顺便记录一下,以便以后 ...

  4. java.lang.Long cannot be cast to java.lang.Integer解决办法

    情景: mybatis连接oracle 报错: 测试增的时候,报错 Java.lang.Long cannot be cast to  java.lang.Integer:删改没有报错. 排查过程: ...

  5. no suitable HttpMessageConverter found for request type [java.lang.Integer]

    今天在使用Spring Template的时候遇到了这个异常: no suitable HttpMessageConverter found for request type [java.lang.I ...

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

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

  7. MyBatis出错Result Maps collection does not contain value for java.lang.Integer

    Servlet.service() for servlet [SpringMVC] in context with path [/eyou] threw exception [Request proc ...

  8. MyBatis查询传一个参数时报错:There is no getter for property named 'sleevetype' in 'class java.lang.Integer

    用MyBatis进行查询,传入参数只有一个时(非Map)如int,报错 There is no getter for property named 'sleevetype' in 'class jav ...

  9. 从源码看java中Integer的缓存问题

    在开始详细的说明问题之前,我们先看一段代码 public static void compare1(){ Integer i1 = 127, i2 = 127, i3 = 128, i4 = 128; ...

  10. Java中Integer的源码学习

      一.开始 public final class Integer extends Number implements Comparable<Integer> 1).由于类修饰符中有关键字 ...

随机推荐

  1. GCD下载图片

    cell.myimage.layer.masksToBounds=YES;     cell.myimage.layer.cornerRadius=cell.myimage.frame.size.wi ...

  2. HTML DOM nodeName nodeValue

    在javascript在,我们得title在标签和文本,它们通常要求这样做 var obj =document.getElementsById("id1"); obj.nodeNa ...

  3. 走向DBA[MSSQL篇] 针对大表 设计高效的存储过程【原理篇】 附最差性能sql语句进化过程客串

    原文:走向DBA[MSSQL篇] 针对大表 设计高效的存储过程[原理篇] 附最差性能sql语句进化过程客串 测试的结果在此处 本篇详解一下原理 设计背景 由于历史原因,线上库环境数据量及其庞大,很多千 ...

  4. 高性能mysql主存架构

    原文:高性能mysql主存架构 MySQL Replication(Master与Slave基本原理及配置) 主从mysql工作原理: 1:过程: (1)Mysql的复制(replication)是一 ...

  5. 30分钟让你了解MongoDB基本操作(转)

    今天记录下MongoDB的基本操作,这只是最基本的,所以是应该掌握的. 数据库 数据库是一个物理容器集合.每个数据库都有自己的一套文件系统上的文件.一个单一的MongoDB服务器通常有多个数据库. 集 ...

  6. 解决 configure.ac:17: error: possibly undefined macro: AC_PROG_LIBTOOL

    当安装configure.ac:17: error: possibly undefined macro: AC_PROG_LIBTOOL If this token and others are le ...

  7. debugging python with IDLE

    1. start IDLE "Python 2.5"→"IDLE(Python GUI)" 2. open your source file window Fr ...

  8. ACM算法

      一.数论算法 1.求两数的最大公约数 2.求两数的最小公倍数 3.素数的求法 A.小范围内判断一个数是否为质数: B.判断longint范围内的数是否为素数(包含求50000以内的素数表): 二. ...

  9. POJ 2081 Recaman&#39;s Sequence(水的问题)

    [简要题意]:这个主题是很短的叙述性说明.挺easy. 不重复. [分析]:只需要加一个判断这个数是否可以是一个数组,这个数组的范围. // 3388K 0Ms #include<iostrea ...

  10. 纠错《COM技术内幕》之ProgID

    近期在看<COM技术内幕>,看到第六章时发现该章节在解释ProgID时有点错误,特此记录一下,也给正在学习COM的小伙伴们一个提示. 并且我发现该问题存在于一些非常多大型软件的COM组件中 ...