equals與”==”

來源:互聯網
上載者:User
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的每個實現情況來具體判斷。
*******************************
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.