網上搜了一遍,對于==和equals的表達感覺不全面;總感覺缺點什麼;今天把這個比較總結出來三條規律。
結論1.基本類型沒有equals方法,隻有==比較,比較的是值。
結論2.所有對象的==比較都是記憶體位址的比較
(上面的兩點簡單不介紹了)
首先我們看Integer類的比較。
1 Integer a=1000;
2 Integer b=1000;
3 System.out.println(a == b);//false
4 System.out.println(a.equals(b));//true
因為a和b都是對象類型,是以都有自己的堆記憶體位址,是以根據結論2得出a==b是false。
至于equals我們看一下源碼

很明顯Integer的equals比較的是值。是以網上有些說法:equals比較的是記憶體位址的說法是以偏概全的;這個equals的比較要根據目前類的equals的實作。
是以a.equals(b)是值的比較。
1 Integer a1=127;
2 Integer a2=127;
3 System.out.println(a1 == a2);//true
4 System.out.println(a1.equals(a2));//true
第三行a1==a2又變成了true;這個似乎違背了結論2.看看源碼吧
1 private static class IntegerCache {
2 static final int low = -128;
3 static final int high;
4 static final Integer cache[];
5
6 static {
7 // high value may be configured by property
8 int h = 127;
9 String integerCacheHighPropValue =
10 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
11 if (integerCacheHighPropValue != null) {
12 try {
13 int i = parseInt(integerCacheHighPropValue);
14 i = Math.max(i, 127);
15 // Maximum array size is Integer.MAX_VALUE
16 h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
17 } catch( NumberFormatException nfe) {
18 // If the property cannot be parsed into an int, ignore it.
19 }
20 }
21 high = h;
22
23 cache = new Integer[(high - low) + 1];
24 int j = low;
25 for(int k = 0; k < cache.length; k++)
26 cache[k] = new Integer(j++);
27
28 // range [-128, 127] must be interned (JLS7 5.1.7)
29 assert IntegerCache.high >= 127;
30 }
31
32 private IntegerCache() {}
33 }
Integer裡面有一個靜态的IntergerCache類,裡面有一個static靜态代碼塊和一個存放Integer的數組cache[].
上面代碼意思是:從jvm中取high值,如果有值的話和127比較取最大值,沒有值的話用127作為最大值。
-128作為最小值。是以cache[]數組的值是從-128~127并且是包裝類型。
回到上面a1==a2的問題為什麼是true的問題。
因為a1和a2的值是127在整型的緩存裡面,是以a1,a2指向的對象都是緩存裡面的對象,是以a1==a2比較的依然是引用,隻不過他們的引用都一樣而已。
如果超過緩存的範圍,就需要重新new了,就會出現新的對象,自然引用就不一樣了;是以a1==a2=1000的包裝類是false.
順便看了一下整型其他包裝類(Byte,Short,Long)的源碼,範圍都是-128~127,并且不可設定的。
Boolean同樣适合上面結論1和結論2.
Float的equals比較:
equals比較的是數值的二進制直接轉換成int型的值比較
Double的equals比較:
equals比較的是數值的二進制直接轉換成long型的值比較
至于我們自定義的類,比如定義一個Student類,其實它們都是調用Object的equals方法。
比較的是對象的引用,也就是記憶體位址的比較。
1 package a;
2
3 public class Student {
4 static class A{
5 @Override
6 public boolean equals(Object obj) {
7 return true;
8 }
9 }
10 static class B{
11
12 }
13
14 public static void main(String[] args) {
15 A a=new A();
16 System.out.println(a.equals(new B()));
17 }
18 }
上面對内部類的A方法重新了equals方法,總是傳回true;那麼傳入任何對象比較,都會是相等的。
結論3:自定義對象的equals比較方式取決于equals方法;如果沒有重寫,比較的就是引用;
如果進行了重寫,那麼比較規則取決于equals體。