Java判等问题:细节决定成败

Java判等问题:细节决定成败

在这里插入图片描述
判等问题,在我们代码中就是一句话的事情,但是这一行代码如果处理不好,不仅会出现致命的bug,下面我们就以Java中 equals、compareTo 和 Java 的数值缓存、字符串驻留等问题展开讨论


1. 注意 equals 和 == 的区别

在业务代码中,我们通常使用 equals 或 == 进行判等操作。equals是方法而 ==是操作符
1.对基本类型,比如 int 、long、进行判断,只能使用 == ,比较对是直接值,因为基本类型对值就是其数值
2.对引用类型,比如Integer 、Long 和 String 进行判等,需要使用 equals 进行内容判等。因为引用类型,需要使用equals进行内容判等。因为饮用类型等直接值是指针,使用 == 的话,比较的是指针,也就是两个对象在内存中的地址,即比较他们是不是同一个对象,而不是比较对象内容
结论:
  • 比较值的内容,除了基本类型只能使用 ==外,其他类型都需要使用 equals。
案例:
public static void main(String[] args) throws Exception {
        Integer a = 127;
        Integer b = 127;
        System.out.println(" a == b " +(a == b));

        Integer c = 128;
        Integer d = 128;
        System.out.println(" c == d " + (c == d));

        Integer g = new Integer(127);
        Integer h = new Integer(127);
        System.out.println(" g == h " + (g == h));

        Integer i = 128;
        int j = 128;
        System.out.println(" i == j " + (i == j));
    }

结果 :

 a == b true
 c == d false
 g == h false
 i == j true
  • 在 a == b 中,编译器会把 a = 127 转换为 Integer.valueOf(127),源码可以发现,这个转换是内部其实做了缓存,使得两个 Integer 指向同一个对象 所以返回true
  public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
  • 在 c == d 中使用128 返回false ,Integer 当不符合-128 127值范围时候。记住用的:new,开辟新的内存空间,不属于IntergerCache管理区
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;
        }


  • 在g == h案例中,New 出来的 Integer 始终是不走缓存的新对象。比较两个新对象,
    或者比较一个新对象和一个来自缓存的对象,结果肯定不是相同的对象,因此返回 false。

2. equals 没有这么简单

如果看过 Object 类源码,你可能就知道,equals 的实现其实是比较对象引用:

public boolean equals(Object obj) {
        return (this == obj);
    }
重点(注意点):
  • 不重写equals方法与“ == ”一样,用于比较对象的引用是否相等。

之所以 Integer 或 String 能通过 equals 实现内容判等,是因为它们都重写了这个方法。

String 的 equals 的实现:

 /**
     * Compares this string to the specified object.  The result is {@code
     * true} if and only if the argument is not {@code null} and is a {@code
     * String} object that represents the same sequence of characters as this
     * object.
     *
     * @param  anObject
     *         The object to compare this {@code String} against
     *
     * @return  {@code true} if the given object represents a {@code String}
     *          equivalent to this string, {@code false} otherwise
     *
     * @see  #compareTo(String)
     * @see  #equalsIgnoreCase(String)
     */
    public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

Integer.equals.()

   /**
     * 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;
    }

个人博客地址:http://blog.yanxiaolong.cn/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值