Java源码解析:深入理解==和equals()
1.結論
1.1 ==
1.所有原始數據類型,比較的是值
2.所有的對象比較的是內存地址
注意點:Integer類有對象池[-128,127]
1.2 equals()方法
1.未重寫的equals()方法,默認比較內存地址
2.String類和原始數據類型中重寫了equals()方法,所有比較的是值,而非內存地址
1.3 建議
因此,要判斷兩個對象是不是同一個對象,最準確比較方式是==
但若想將屬性值相同的兩個對象,判斷為同一個對象,則建議用重寫的equals()方法【比如set集合中去重】
2. 一起看源碼
== 是Java內部定義的,沒有源碼可看,就不管了。這里主要看一下equals()方法的源碼。
原始數據類類型對應的包裝類的equals方法中都有一句話如下所示:
The result is {@code true} if and only if the argument is not {@code null} and is a {@code 包裝類} object that represents a {@code 對應原始數據類型} with the same value as the {@code 對應原始數據類型} represented by this object.
意思是:只要包裝類對應的原始數據類型的值是一樣的,那么equals返回true。(前提:對象不為null)
Character類
Byte類
Short類
Integer類
Long類
Float類
/*** Compares this object against the specified object. The result* is {@code true} if and only if the argument is not* {@code null} and is a {@code Float} object that* represents a {@code float} with the same value as the* {@code float} represented by this object. For this* purpose, two {@code float} values are considered to be the* same if and only if the method {@link #floatToIntBits(float)}* returns the identical {@code int} value when applied to* each.** <p>Note that in most cases, for two instances of class* {@code Float}, {@code f1} and {@code f2}, the value* of {@code f1.equals(f2)} is {@code true} if and only if** <blockquote><pre>* f1.floatValue() == f2.floatValue()* </pre></blockquote>** <p>also has the value {@code true}. However, there are two exceptions:* <ul>* <li>If {@code f1} and {@code f2} both represent* {@code Float.NaN}, then the {@code equals} method returns* {@code true}, even though {@code Float.NaN==Float.NaN}* has the value {@code false}.* <li>If {@code f1} represents {@code +0.0f} while* {@code f2} represents {@code -0.0f}, or vice* versa, the {@code equal} test has the value* {@code false}, even though {@code 0.0f==-0.0f}* has the value {@code true}.* </ul>** This definition allows hash tables to operate properly.** @param obj the object to be compared* @return {@code true} if the objects are the same;* {@code false} otherwise.* @see java.lang.Float#floatToIntBits(float)*/public boolean equals(Object obj) {return (obj instanceof Float)&& (floatToIntBits(((Float)obj).value) == floatToIntBits(value));}Double類
/*** Compares this object against the specified object. The result* is {@code true} if and only if the argument is not* {@code null} and is a {@code Double} object that* represents a {@code double} that has the same value as the* {@code double} represented by this object. For this* purpose, two {@code double} values are considered to be* the same if and only if the method {@link* #doubleToLongBits(double)} returns the identical* {@code long} value when applied to each.** <p>Note that in most cases, for two instances of class* {@code Double}, {@code d1} and {@code d2}, the* value of {@code d1.equals(d2)} is {@code true} if and* only if** <blockquote>* {@code d1.doubleValue() == d2.doubleValue()}* </blockquote>** <p>also has the value {@code true}. However, there are two* exceptions:* <ul>* <li>If {@code d1} and {@code d2} both represent* {@code Double.NaN}, then the {@code equals} method* returns {@code true}, even though* {@code Double.NaN==Double.NaN} has the value* {@code false}.* <li>If {@code d1} represents {@code +0.0} while* {@code d2} represents {@code -0.0}, or vice versa,* the {@code equal} test has the value {@code false},* even though {@code +0.0==-0.0} has the value {@code true}.* </ul>* This definition allows hash tables to operate properly.* @param obj the object to compare with.* @return {@code true} if the objects are the same;* {@code false} otherwise.* @see java.lang.Double#doubleToLongBits(double)*/public boolean equals(Object obj) {return (obj instanceof Double)&& (doubleToLongBits(((Double)obj).value) ==doubleToLongBits(value));} 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的Java源码解析:深入理解==和equals()的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java源码解析:hashCode与相同
- 下一篇: lombok快速入门:实体类中再也不用写