java中的equals()方法

Java的基类(Object类)提供了一些方法,其中equals()方法用于判断两个对象是否相等。

equals()方法不是final方法,可以被重写(Overwrite),因此各个引用类型的equals()方法的实现不尽相同。

Object类中的equals()方法

Object类中的equals()方法实现如下:

/**
 * Indicates whether some other object is "equal to" this one.
 * 

* The {@code equals} method implements an equivalence relation * on non-null object references: *

    *
  • It is reflexive: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}. *
  • It is symmetric: for any non-null reference values * {@code x} and {@code y}, {@code x.equals(y)} * should return {@code true} if and only if * {@code y.equals(x)} returns {@code true}. *
  • It is transitive: for any non-null reference values * {@code x}, {@code y}, and {@code z}, if * {@code x.equals(y)} returns {@code true} and * {@code y.equals(z)} returns {@code true}, then * {@code x.equals(z)} should return {@code true}. *
  • It is consistent: for any non-null reference values * {@code x} and {@code y}, multiple invocations of * {@code x.equals(y)} consistently return {@code true} * or consistently return {@code false}, provided no * information used in {@code equals} comparisons on the * objects is modified. *
  • For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}. *
*

* The {@code equals} method for class {@code Object} implements * the most discriminating possible equivalence relation on objects; * that is, for any non-null reference values {@code x} and * {@code y}, this method returns {@code true} if and only * if {@code x} and {@code y} refer to the same object * ({@code x == y} has the value {@code true}). *

* Note that it is generally necessary to override the {@code hashCode} * method whenever this method is overridden, so as to maintain the * general contract for the {@code hashCode} method, which states * that equal objects must have equal hash codes. * * @param obj the reference object with which to compare. * @return {@code true} if this object is the same as the obj * argument; {@code false} otherwise. * @see #hashCode() * @see java.util.HashMap */ public boolean equals(Object obj) { return (this == obj); }

从实现上可以看出,Object类的equals()方法实现采用的是区分度最高的算法,即只要两个对象不是同一个对象(同一个内存地址),那么equals()就一定返回false。

Object obj = new Object();
Object obj1 = new Object();
System.out.println(obj.equals(obj1)); // false

虽然我们在定义类(Java中所有类都默认继承自Object类)时,可以重写equals()方法,但是JDK中(在方法的注释上)说明了实现equals()方法应该遵守的一些约定。

1.自反性(Reflexive):x.equals(x)必须返回true。

2.对称性(Symmetric):x.equals(y)与y.equals(x)的返回值必须相等。

3.传递性(Transitive):如果x.equals(y)为true,y.equals(z)也为true,那么x.equals(z)必须为true。

4.一致性(Consistence):如果对象x和y在equals()中使用的信息都没有改变,那么x.equals(y)值始终不变。

5.非null(Non-null):如果x不是null,y为null,则x.equals(y)必须为false。

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

可以看出,String类中的equals()方法内部采用的是每个字符逐一做比较的算法,意思就是,只要两个字符串的值相等而不需要内存地址也相等,equals()方法的结果就是true。

String str = "yanggb";
String str1 = new String("yanggb");
System.out.println(str == str1); // false
System.out.println(str.equals(str1)); // true

String类是比较特殊的,因为JVM中有一个字符串常量池的概念,相同的字面量声明实际上都是指向的同一个内存地址(都在字符串常量池中),只有new出来的String对象,才是在堆中开辟空间,并指向不同的内存地址。

 

"时间会消磨掉所有的热情,曾经歇斯底里的执着,最终都变成了可有可无的消遣。"

你可能感兴趣的:(java中的equals()方法)