Java 之Integer相等比较

1.问题提出

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

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默认的装箱的实现方式:ui

 /**
     * 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),一旦是建立的对象,那么比较的是对象天然是不相等,即便值是相等的。this

3.其余

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

  由于这个方法取得是具体的值,因此无论是多少,具体的结果老是相等的。spa

相关文章
相关标签/搜索