一道经典的Java面试题:equals ,== 和hashcode()的区别

==

对于基本类型是值比较,对于引用类型来讲是引用比较。java

/**
     * == 的比较
     */
    @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方法.bash

先看看Obeject重写的equals方法:ui

//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的比较this

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,spa

//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()的简单使用code

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。其结构以下:cdn

hashcode()相等,那么它们具备 相同的桶的位置,此时就如Entry1和Entry2,可是,Entry1和Entry2的equals并不必定想等,这是再举个例子Entry1=abc,Entry2=abc,那么它们是相等的,可是Entry1=abc,Entry2=def,那么它们是不相等的. equals相等,那么说明它们在同一列上,那意味着桶的位置同样,则.hashCode()确定相同对象

相关文章
相关标签/搜索