Java的基類(Object類)提供了一些方法,其中equals()方法用於判斷兩個對象是否相等。
equals()方法不是final方法,可以被重寫(Overwrite),因此各個引用類型的equals()方法的實現不盡相同。
Object類中的equals()方法
Object類中的equals()方法實現如下:
/** * Indicates whether some other object is "equal to" this one. * <p> * The {@code equals} method implements an equivalence relation * on non-null object references: * <ul> * <li>It is <i>reflexive</i>: for any non-null reference value * {@code x}, {@code x.equals(x)} should return * {@code true}. * <li>It is <i>symmetric</i>: 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}. * <li>It is <i>transitive</i>: 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}. * <li>It is <i>consistent</i>: 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. * <li>For any non-null reference value {@code x}, * {@code x.equals(null)} should return {@code false}. * </ul> * <p> * 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}). * <p> * 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對象,才是在堆中開辟空間,並指向不同的內存地址。
"時間會消磨掉所有的熱情,曾經歇斯底里的執着,最終都變成了可有可無的消遣。"