java中 hashCode() 和 equals()


1. 值类型是存储在内存中的栈,而引用类型的变量在栈中仅仅是存储引用类型变量的地址来自堆,而其本身则存储在栈中。

2. ==操作比较的是两个变量的值是否相等,

3. 对于引用型变量表示的是两个变量在堆中存储的地址是否相同,即栈中的内容是否相同。


 

Object:

  1-----public native int hashCode();

    该方法返回的值一般是通过将该对象的内部地址转换成一个整数来实现的。

    这样能保证每个对象的哈希码值不一样。

    native:方法修饰符 ——(标志外部已实现)

    Native方法是由另外一种语言(如c/c++,汇编)实现的本地方法。

    因为在外部实现了方法,所以在java代码中,就不需要声明了,有点类似于借口方法。

    Native可以和其他一些修饰符连用,

    但是abstract方法和Interface方法不能用native来修饰,

 

  

  2-----public boolean equals(Object obj) {
      return (this == obj);
       }

    很明显是对两个对象的地址值进行的比较(即比较引用是否相同)。


  由于String 、Math、还有Integer、Double。。。。

  等这些封装类在使用equals()方法时,已经覆盖了object类的equals()方法。


 

  Integer 和 String中的。hashCode()、equals();

Integer:

  public boolean equals(Object obj) {
      if (obj instanceof Integer) {
          return value == ((Integer)obj).intValue();
      }
      return false;
    }

    public int hashCode() {
    return value;       
    }

String:

   public int hashCode() {
    int h = hash;
    if (h == 0) {         
//这就是为什么在集合set在保证一般元素无异性 比较hashcode时会很高效的原因的来源 int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h; } public boolean equals(Object anObject) {    if (this == anObject) { return true;    }    if (anObject instanceof String) {    String anotherString = (String)anObject;    int n = count;    if (n == anotherString.count) {    char v1[] = value;    char v2[] = anotherString.value;    int i = offset;    int j = anotherString.offset;    while (n-- != 0) {    if (v1[i++] != v2[j++])    return false;    }    return true;    }    }    return false; }

 


 

示例:

 

public class HashCodeAndEquals {
    public static void main(String args[])
    {
        test1();
        test2();
        test3();
    }
    public static void test1()
    {
        Integer a1 = new Integer(182);
        Integer a2 = new Integer(182);
        System.out.println(a1 == a2);   
   // false System.out.println(a1.equals(a2));
     // true System.out.println(a1.hashCode() == a2.hashCode()) ;
   // true System.out.println(); } public static void test2() { Integer a1 = new Integer(182); String s2 = new String("182"); System.out.println(a1.equals(s2)); //if (anObject instanceof String) 直接 return false, // instanceof 表示是否是该类的一个示例, System.out.println(); } public static void test3() { String s1 = new String("182"); String s2 = new String("182"); System.out.println(s1 == s2);
// 栈值比较 false System.out.println(s1.equals(s2));
// true System.out.println(s1.hashCode() == s2.hashCode()) ;
// 因为覆写了hashcode,实体内同容 true T t1 = new T(); T t2 = new T(); System.out.println(t1.hashCode() == t2.hashCode()) ;
   // 地址映射比较 false } } class T{ }

 

 

 


 

总结:

  由上面我们可以知道

  • 自定义类如无重写equals方法,equals和==是等价的。
  • 如果没有重写hashCode() 则是对引用型数据内存地址的一个映射的比较,肯定不同
  • Integer的 hashCode和equals则都是比较 值:
  • String的equals则是比较实体是否相同,而hashCode则是一个 value[] 值的哈希值
  • (String实例极小极小可能会出现hashCode值相同,但equals不同)
    (但是,equals相同时,hashCode一定相同)

  

 

 

 

posted @ 2013-11-09 15:22  寻影  阅读(350)  评论(2编辑  收藏  举报