美文网首页
java中==和equals和hashCode的区别

java中==和equals和hashCode的区别

作者: 丶丶TkoRn丶丶 | 来源:发表于2020-02-19 16:34 被阅读0次
    • ==

    1、基础数据类型,== 比较的是他们的值,如:byte,short,char,int,long,float,double,boolean

    2、引用类型,== 比较的是对象的存放地址,所以除非同一个new出来的对象比较为true否则都是false。
    总结:对象的内存地址正是对象在栈中存放的引用地址,由此可见 == 是对栈中的值进行比较。若需要比较对象堆中的值需要重写equals方法。

    • equals
      1、没有重写,Object 的equals() 比较的是对象的存放地址,于 == 是一样的。
      public boolean equals(Object obj) {
            return (this == obj);
      }
      
      2、重写之后就得看具体的代码,一般是比较堆内存中对象的值,如String类
      public boolean equals(Object anObject) {
            if (this == anObject) {//同一个对象
                return true;
            }
            if (anObject instanceof String) {
                String anotherString = (String)anObject;
                int n = length();
                if (n == anotherString.length()) {//长度相同
                    int i = 0;
                    while (n-- != 0) {
                        //判断对应的字节
                        if (charAt(i) != 
                        anotherString.charAt(i))
                            return false;
                        i++;
                    }
                    return true;
                }
            }
            return false;
      }
      
    • hashCode

    1、hashCode()方法返回一个对象的哈希码值,据此很容易推断出,我们需要每个对象的hash码尽可能不同,这样才能保证散列的存取性能。事实上,Object类提供的默认实现确实保证每个对象的hash码不同(在对象的内存地址基础上经过特定算法返回一个hash码)。

    2、为什么jdk中希望我们重写equals() 时,非常有必要重写hashCode() ?
    因为jdk希望两个对象 equals 为true的同时他们 hashCode() 的值是相等的,如果不这样的话无法保证对象在散列正常运行。

    3、怎么重写 hashCode() ?
    重写是需注意遵循 hashCode 公约:
    1. 在应用程序的执行期间,只要对象的equals方法的比较操作所用到的信息没有被修改,那么对这同一个对象调用多次,hashCode方法都必须始终如一地返回同一个整数。在同一个应用程序的多次执行过程中,每次执行所返回的整数可以不一致。
    2. 如果两个对象根据equals()方法比较是相等的,那么调用这两个对象中任意一个对象的hashCode方法都必须产生同样的整数结果。
    3. 如果两个对象根据equals()方法比较是不相等的,那么调用这两个对象中任意一个对象的hashCode方法,则不一定要产生相同的整数结果。但是程序员应该知道,给不相等的对象产生截然不同的整数结果,有可能提高散列表的性能。

    建议借鉴String类,甚至可以直接拿来用。

        /**
         * Returns a hash code for this string. The hash code for a
         * {@code String} object is computed as
         * <blockquote><pre>
         * s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
         * </pre></blockquote>
         * using {@code int} arithmetic, where {@code s[i]} is the
         * <i>i</i>th character of the string, {@code n} is the length of
         * the string, and {@code ^} indicates exponentiation.
         * (The hash value of the empty string is zero.)
         *
         * @return  a hash code value for this object.
         */
        public int hashCode() {
                int h = hash;
                final int len = length();
                if (h == 0 && len > 0) {
                    for (int i = 0; i < len; i++) {
                        h = 31 * h + charAt(i);
                    }
                    hash = h;
                }
                return h;
        }
    

    总结

    1. == 是对栈中的值进行比较。
    2. equals 是提供给我们重写的,一般用来对堆中的值(对象)进行比较。
    3. hashCode 是提供给hashMap 做key使用的,为了保证对象在hashMap中正常运行,所以在重写equals时需要重新hashCode。

    相关文章

      网友评论

          本文标题:java中==和equals和hashCode的区别

          本文链接:https://www.haomeiwen.com/subject/gygofhtx.html