Java中== equals hashcode解析

来源:互联网 发布:sql回滚语句 编辑:程序博客网 时间:2024/04/28 11:16

1. '=='是用来比较两个变量(基本类型和对象类型)的值是否相等的, 如果两个变量是基本类型的,那很容易,直接比较值就可以了。如果两个变量是对象类型的,那么它还是比较值,只是它比较的是这两个对象在栈中的引用(即地址)。

对象是放在堆中的,栈中存放的是对象的引用(地址)。由此可见'=='是对栈中的值进行比较的。如果要比较堆中对象的内容是否相同,那么就要重写equals方法了。
2. Object类中的equals方法就是用'=='来比较的,所以如果没有重写equals方法,equals和==是等价的。
通常我们会重写equals方法,让equals比较两个对象的内容,而不是比较对象的引用(地址)因为往往我们觉得比较对象的内容是否相同比比较对象的引用(地址)更有意义。
3. Object类中的hashCode是返回对象在内存中地址转换成的一个int值(可以就当做地址看)。所以如果没有重写hashCode方法,任何对象的hashCode都是不相等的。通常在集合类的时候需要重写hashCode方法和equals方法,因为如果需要给集合类(比如:HashSet)添加对象,那么在添加之前需要查看给集合里是否已经有了该对象,比较好的方式就是用hashCode。
4. 注意的是String、Integer、Boolean、Double等这些类都重写了equals和hashCode方法,这两个方法是根据对象的内容来比较和计算hashCode的。(详细可以查看jdk下的String.java源代码),所以只要对象的基本类型值相同,那么hashcode就一定相同。
5. equals()相等的两个对象,hashcode()一般是相等的,最好在重写equals()方法时,重写hashcode()方法; equals()不相等的两个对象,却并不能证明他们的hashcode()不相等。换句话说,equals()方法不相等的两个对象,hashcode()有可能相等。 反过来:hashcode()不等,一定能推出equals()也不等;hashcode()相等,equals()可能相等,也可能不等。在object类中,hashcode()方法是本地方法,返回的是对象的引用(地址值),而object类中的equals()方法比较的也是两个对象的引用(地址值),如果equals()相等,说明两个对象地址值也相等,当然hashcode()也就相等了。

以下是测试代码。

 public class Equals_HashCode {    public static void main(String[] args) {                 String a = new String("str");         String b = new String("str");         System.out.println(a==b);         System.out.println(a.equals(b));         System.out.println(a.hashCode());         System.out.println(b.hashCode());         // 输出 false true 114225 114225         class A{             String str;             int i;             public A(String str, int i) {                 super();                 this.str = str;                 this.i = i;             }         }         A aA = new A("str",1);         A bA = new A("str",1);         System.out.println(aA==bA);         System.out.println(aA.equals(bA));         System.out.println(aA.hashCode());         System.out.println(bA.hashCode());         // 输出 false false 6413875 21174459         class B{             String str;             public B(String str){                 this.str = str;             }         }         B aB = new B("str");         B bB = new B("str");         System.out.println(aB==bB);         System.out.println(aB.equals(bB));         System.out.println(aB.hashCode());         System.out.println(bB.hashCode());         // 输出 false false 827574 17510567         class C{             int i;             public C(int i){                 this.i = i;             }         }         C aC = new C(1);         C bC = new C(1);         System.out.println(aC==bC);         System.out.println(aC.equals(bC));         System.out.println(aC.hashCode());         System.out.println(bC.hashCode());         //输出 false false 27744459  28737396    } } 

后来又有一个问题,看到了下面的这样一句话:

equals()相等的两个对象,hashcode()一定相等; equals()不相等的两个对象,却并不能证明他们的hashcode()不相等。

去网上查看了下,还没有很合适能说服我的答案,不过有下面些话,能印证一些。

在java中,equals和hashcode是有设计要求的,equals相等,则hashcode一定相等,反之则不然。

为何会有这样的要求?

在集合中,比如HashSet中,要求放入的对象不能重复,怎么判定呢?

首先会调用hashcode,如果hashcode相等,则继续调用equals,也相等,则认为重复。

如果重写equals后,如果不重写hashcode,则hashcode就是继承自Object的,返回内存编码,这时候可能出现equals相等,而hashcode不等,你的对象使用集合时,就会等不到正确的结果。
public V put(K key, V value) {        if (key == null)            return putForNullKey(value);        int hash = hash(key.hashCode());        int i = indexFor(hash, table.length);        for (Entry<K,V> e = table[i]; e != null; e = e.next) {            Object k;            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }         modCount++;        addEntry(hash, key, value, i);        return null;    }



0 0
原创粉丝点击