国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

equals與==操作符的比較

2019-11-18 14:43:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

  equals方法是Object類的一個(gè)方法,所有繼續(xù)自O(shè)bject類的類都會(huì)集成此方法,并且可以重載這個(gè)方法來(lái)實(shí)現(xiàn)各自的比較操作,而且jdk也正是推薦這種做法。所以開(kāi)發(fā)人員盡可以在自己的類中實(shí)現(xiàn)自己的equals方法來(lái)完成自己特定的比較功能,所以各個(gè)類的equals方法與= =之間并沒(méi)有絕對(duì)的關(guān)系,這要根據(jù)各自類中自己的實(shí)現(xiàn)情況來(lái)看。也就是說(shuō)可能會(huì)有兩種情況發(fā)生:equals方法和= =相同或者不相同。在多數(shù)情況下這兩者的區(qū)別就是究竟是對(duì)對(duì)象的引用進(jìn)行比較還是對(duì)對(duì)象的值進(jìn)行比較(其他非凡情況此處不予考慮)。那么= =操作符是比較的什么呢?= =操作符是比較的對(duì)象的引用而不是對(duì)象的值。并且由下面的源代碼可以看出在最初的Object對(duì)象中的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方法和 = =操作符是完成了相同的比較功能,都是對(duì)對(duì)象的引用進(jìn)行了比較。那么我們熟悉的String類的equals方法是對(duì)什么內(nèi)容進(jìn)行比較的呢?下面我們來(lái)看它的代碼和注釋:
源代碼:
-------------------------------------------------------------
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;

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 旅游| 聂拉木县| 馆陶县| 响水县| 长沙县| 彭泽县| 郸城县| 东乌| 封开县| 荥经县| 铜川市| 茂名市| 武功县| 奉新县| 新巴尔虎右旗| 邻水| 大同县| 凤冈县| 汝南县| 类乌齐县| 岳阳市| 吴旗县| 铁岭市| 佛教| 蒙城县| 乃东县| 桐梓县| 镶黄旗| 临汾市| 桐庐县| 十堰市| 静乐县| 夹江县| 从化市| 浦北县| 湘阴县| 巴塘县| 津南区| 余江县| 宝鸡市| 台江县|