Integer 值判断相等

案例:

public class Test {
    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        System.out.println("a == b :"+ (a == b));
        System.out.println("a.equals(b):"+a.equals(b));
        String x = "127";
        String y = "127";
        System.out.println("Integer.valueOf(x) == Integer.valueOf(y) :" + (Integer.valueOf(x) == Integer.valueOf(y)));
        System.out.println("Integer.valueOf(x).equals(Integer.valueOf(y)):"+Integer.valueOf(x).equals(Integer.valueOf(y)));
        System.out.println("====================================================================");
        Integer a1 = 128;
        Integer b1 = 128;
        System.out.println("a1 == b:"+(a1 == b1));
        System.out.println("a.equals(b):"+a.equals(b1));
        String x1 = "128";
        String y1 = "128";
        System.out.println("Integer.valueOf(x1) == Integer.valueOf(y1) :" + (Integer.valueOf(x1) == Integer.valueOf(y1)));
        System.out.println("Integer.valueOf(x1).equals(Integer.valueOf(y1)):"+Integer.valueOf(x1).equals(Integer.valueOf(y1)));

    }
}

日志打印:

a == b :true
a.equals(b):true
Integer.valueOf(x) == Integer.valueOf(y) :true
Integer.valueOf(x).equals(Integer.valueOf(y)):true
====================================================================
a1 == b:false
a.equals(b):false
Integer.valueOf(x1) == Integer.valueOf(y1) :false
Integer.valueOf(x1).equals(Integer.valueOf(y1)):true

通过案例发现,值为127不管是 Integer 还是 String 类型,== 和 equals 都能比较成功。128与类型无关,与比较的方法有关。

equals

通过查看源码发现 Integer 重写的 equals、hashCode 方法,所以使用 equals 方法比较大小母庸质疑为 true.

/**
     * Returns a hash code for this {@code Integer}.
     *
     * @return  a hash code value for this object, equal to the
     *          primitive {@code int} value represented by this
     *          {@code Integer} object.
     */
    @Override
    public int hashCode() {
        return Integer.hashCode(value);
    }

    /**
     * Returns a hash code for a {@code int} value; compatible with
     * {@code Integer.hashCode()}.
     *
     * @param value the value to hash
     * @since 1.8
     *
     * @return a hash code value for a {@code int} value.
     */
    public static int hashCode(int value) {
        return value;
    }

    /**
     * Compares this object to the specified object.  The result is
     * {@code true} if and only if the argument is not
     * {@code null} and is an {@code Integer} object that
     * contains the same {@code int} value as this object.
     *
     * @param   obj   the object to compare with.
     * @return  {@code true} if the objects are the same;
     *          {@code false} otherwise.
     */
    public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
==

127和127比较返回true,128和128比较返回false,有点出乎意料,主要是因为我们使用了惯用思维“以为/觉得”他们相等,没有经过认证。Integer a = 127; 是自动装箱会调用Interger.valueOf(int)方法;

public static Integer valueOf(String s) throws NumberFormatException {
        return Integer.valueOf(parseInt(s, 10));
    }
	
	public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }

默认 IntegerCache.low 是-127,Integer.high是128,如果在这个区间内,他就会把变量i当做一个变量,放到内存中;但如果不在这个范围内,就会去new一个 Integer 对象,所以使用 “==” 比较127返回true,比较128返回 false。