equals與"=="操作符的比較
--------------------------------------------------------------------------------
equals方法是Object類的一個方法,所有繼續自Object類的類都會集成此方法,並且可以重載這個方法來實現各自的比較操作,而且jdk也正是推薦這種做法。所以開發人員盡可以在自己的類中實現自己的equals方法來完成自己特定的比較功能,所以各個類的equals方法與= =之間並沒有絕對的關系,這要根據各自類中自己的實現情況來看。也就是說可能會有兩種情況發生:equals方法和= =相同或者不相同。在多數情況下這兩者的區別就是究竟是對對象的引用進行比較還是對對象的值進行比較(其他非凡情況此處不予考慮)。那麼= =操作符是比較的什麼呢?= =操作符是比較的對象的引用而不是對象的值。並且由下面的源代碼可以看出在最初的Object對象中的equals方法是與= =操作符完成功能是相同的。
源碼:
Java.lang.Object.equals()方法:
-------------------------------------------------------------
public boolean equalss(Object obj) {
return (this = = obj);
}
-------------------------------------------------------------
jdk文檔中給出如下解釋:
-------------------------------------------------------------
The equalss method implements an equivalence relation:
? It is reflexive: for any reference value x, x.equalss(x) should return true.
? It is symmetric: for any reference values x and y, x.equalss(y) should return true if and only if y.equalss(x) returns true.
? It is transitive: for any reference values x, y, and z, if x.equalss(y) returns true and y.equalss(z) returns true, then x.equalss(z) should return true.
? It is consistent: for any reference values x and y, multiple invocations of x.equalss(y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified.
? For any non-null reference value x, x.equalss(null) should return false.
The equalss method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equals objects must have equals hash codes.
-------------------------------------------------------------
由以上的注釋可知equals方法和 = =操作符是完成了相同的比較功能,都是對對象的引用進行了比較。那麼我們熟悉的String類的equals方法是對什麼內容進行比較的呢?下面我們來看它的代碼和注釋:
源代碼:
-------------------------------------------------------------
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = count;
if (n == anotherString.count) {
char v1[] = value;
char v2[] = anotherString.value;
int i = offset;
int j = anotherString.offset;
while (n-- != 0) {
if (v1[i++] != v2[j++])
return false;
}
return true;
}
}
return false;
}
-------------------------------------------------------------
此方法的注釋為:
-------------------------------------------------------------
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
-------------------------------------------------------------
由上面的代碼和注釋可以得到String類的equal方法是對對象的值進行比較。
根據以上的討論可以得出結論:equal方法和= =操作符是否存在區別要個別對待,要根據equal的每個實現情況來具體判定。
*******************************
belter(
[email protected])
none.blogdriver.com