1. 程式人生 > >一道經典的Java面試題:equals ,== 和hashcode()的區別

一道經典的Java面試題:equals ,== 和hashcode()的區別

==

對於基本型別是值比較,對於引用型別來說是引用比較。

	/**
     * == 的比較
     */
    @Test
    public void testOne(){
        int a = 200;
        int b = 200;
        Integer c = 200;
        Integer d = 200;
        //值比較
        System.out.println(a == b);//同基本型別同值比較:true
        //引用型別比較
        System.out.println(c == d);//false
    }

equals

equals是原始類Object的方法,即所有物件都有equals方法,預設情況下(即沒有重寫)是引用比較,但是JDK中類很多重寫了equals方法(一般是先進行 == 比較,再判斷是否要進行值比較),所以一般情況下是值比較,注:基本型別不能使用equals比較,而是用 == ,因為基本型別沒有equals方法.

先看看Obeject重寫的equals方法:

//Object:
public boolean equals(Object obj) {
        return (this == obj);
    }
    
 //Integer :
 //先判斷是否為同一型別,不是直接false,是的話在進行值比較
public boolean equals(Object obj) {
        if (obj instanceof Integer) {
            return value == ((Integer)obj).intValue();
        }
        return false;
    }
    
//String:
//先比較地址,然後判斷是否為同一型別,不是直接false,是的話在進行值比較
public boolean equals(Object anObject) {
        if (this == anObject) {
            return true;
        }
        if (anObject instanceof String) {
            String anotherString = (String)anObject;
            int n = value.length;
            if (n == anotherString.value.length) {
                char v1[] = value;
                char v2[] = anotherString.value;
                int i = 0;
                while (n-- != 0) {
                    if (v1[i] != v2[i])
                        return false;
                    i++;
                }
                return true;
            }
        }
        return false;
    }

equals的比較

class Cat{
    String name = "cat";
}
class Dog{
    String name = "dog";
}
________________________________________________________________________
	/**
     * euqals 比較
     */
    @Test
    public void testTwo(){
        int a = 200;
        int b = 200;
        Integer c = 300;
        Integer d = 300;
        Cat cat = new Cat();
        Dog dog = new Dog();
        System.out.println(c.equals(a));//false
        System.out.println(c.equals(d));//true
        //System.out.println(a.equals(cat));//基本型別不能使用equals比較,而是用==,因為基本型別沒有equals方法
    }

hashcode()

hashcode()也是Object的方法,是一個本地方法,用C/C++語言實現的,由java去呼叫返回的物件的地址值。但JDK中很多類都對hashcode()進行了重寫。比如Boolean的表示true則雜湊值為1231,表示false則雜湊值為1237,

	//Object:
	public native int hashCode();
	
	//Integer直接返回值
    public int hashCode() {
        return Integer.hashCode(value);
    }
    public static int hashCode(int value) {
        return value;
    }
    
	//String 返回此字串的雜湊碼。
	public int hashCode() {
        int h = hash;
        if (h == 0 && value.length > 0) {
            char val[] = value;

            for (int i = 0; i < value.length; i++) {
                h = 31 * h + val[i];
            }
            hash = h;
        }
        return h;
    }

hashcode()的簡單使用

class Cat{
    String name = "cat";
}
class Dog{
    String name = "dog";
}
---------------------------------------------------------------------
	@Test
    public void testThree(){
        Cat cat = new Cat();
        Dog dog = new Dog();
        System.out.println(cat.hashCode());//204349222
        System.out.println(dog.hashCode());//231685785
        Integer a = 200;
        Integer b = 300;
        System.out.println(a.hashCode());//200
        System.out.println(b.hashCode());//300
    }

equals 與 == ,hashcode()的比較

  1. equal()相等的兩個物件他們的hashCode()肯定相等。
  2. hashCode()相等的兩個物件他們的equal()不一定相等。

可以嘗試使用聯想法去記住以上概念。比如我們使用的HashMap。其結構如下:

hashcode()相等,那麼它們具有 相同的桶的位置,此時就如Entry1和Entry2,但是,Entry1和Entry2的equals並不一定想等,這是再舉個例子Entry1=abc,Entry2=abc,那麼它們是相等的,但是Entry1=abc,Entry2=def,那麼它們是不相等的. equals相等,那麼說明它們在同一列上,那意味著桶的位置一樣,則.h