A classic Java interview questions: the difference between equals, == and hashcode () in

==

For primitive values ​​are compared as a reference to compare it to a reference type.

    /**
     * == 的比较
     */
    @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

Object class equals the original method, i.e., all objects have the equals method, default (i.e. not rewritable) is a reference comparison, but many of the JDK class overrides the equals method (usually to be == comparison, then determines whether to perform the comparison value), it is generally value comparison, note: can not be compared using equals basic types, but with ==, because there is no basic types equals method.

Take a look at rewriting the equals method Obeject:

//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;
    }

The comparison of 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 () method also Object is a native method, implemented with a C / C ++ language, to call a java object returned address value. But many JDK classes of hashcode () has been rewritten. Such as Boolean true representation of the hash value 1231, the hash value 1237 represents false,

    //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 () is simple to use :

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 and ==, hashcode () comparison :

  1. equal () two objects are equal in their hashCode () is certainly equal.
  2. hashCode () is equal to two objects of their equal () are not necessarily equal.

Lenovo may try to use the law to bear in mind the above concepts. For example, we use HashMap. The following structure:

A classic Java interview questions: the difference between equals, == and hashcode () in

hashcode () are equal, they have the same position of the bucket , and this time just as Entry1 Entry2, however, equals Entry2 Entry1 and not necessarily want to wait, which is another example Entry1 = abc, Entry2 = abc, then they are equal, but Entry1 = abc, Entry2 = def, then they are not equal.
equal equals, then shows them on the same column , that means the same as the position of the bucket, then .hashCode () is certainly the same

Guess you like

Origin blog.51cto.com/14226230/2422689