hashcode 和 System.identityHashCode

openjdk源码:http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/5b86f66575b7

小例子

public class TestHashCode {
    public static void main(String[] args) {
        TestHashCode test1 = new TestHashCode();
        TestHashCode test2 = new TestHashCode();
        
        System.out.println("test1.hashcode:" + test1.hashCode());
        System.out.println("test2.hashcode:" + test2.hashCode());
        
        System.out.println("test1.identityHashCode:" + System.identityHashCode(test1));
        System.out.println("test2.identityHashCode:" + System.identityHashCode(test2));
    }
}

输出:

test1.hashcode:366712642
test2.hashcode:1829164700
test1.identityHashCode:366712642
test2.identityHashCode:1829164700

可以看到同一个对象调用hashCode方法和identityHashCode的结果是一样的.

稍微改动一下
public class TestHashCode {
    
    public int hashCode() {
        return 3;
    }
    public static void main(String[] args) {
        TestHashCode test1 = new TestHashCode();
        TestHashCode test2 = new TestHashCode();
        
        System.out.println("test1.hashcode:" + test1.hashCode());
        System.out.println("test2.hashcode:" + test2.hashCode());
        
        System.out.println("test1.identityHashCode:" + System.identityHashCode(test1));
        System.out.println("test2.identityHashCode:" + System.identityHashCode(test2));
    }
}

输出:

test1.hashcode:3
test2.hashcode:3
test1.identityHashCode:366712642
test2.identityHashCode:1829164700
问题:

可以看到如果子类重写了hashCode方法之后,调用自身的hashCode的方法时运行时多态就会调用自身的已经重写的hashCode方法,因此对于hashcode方法时结果的改变在我们的预期之内,但是对于System.identityHashCode(Object obj)方法没有改变?如果他们之间存在什么关系,我们需要看一下?

identityHashCodehashCode()

看看identityHashCode的定义
需要注意两点:

1.定义里面写着不论类是否重写了hashCode()方法,它都会返回默认的哈希值(其实就是本地方法的值)
2.是属于System类里面的static方法

 /**
     * Returns the same hash code for the given object as
     * would be returned by the default method hashCode(),
     * whether or not the given object's class overrides
     * hashCode().
     * The hash code for the null reference is zero.
     *
     * @param x object for which the hashCode is to be calculated
     * @return  the hashCode
     * @since   JDK1.1
     */
    public static native int identityHashCode(Object x);

对应的本地代码和代码链接

JNIEXPORT jint JNICALL
Java_java_lang_System_identityHashCode(JNIEnv *env, jobject this, jobject x)
{
 return JVM_IHashCode(env, x);
}

看看hashCode()方法
注意与identityHashCode的区别:

1. hashCode()Object类的方法,注意一个类都会直接或者间接继承Object

 public native int hashCode();

对应的本地方法代码和代码链接:

static JNINativeMethod methods[] = {
{"hashCode",    "()I",                    (void *)&JVM_IHashCode},
{"wait",        "(J)V",                   (void *)&JVM_MonitorWait},
{"notify",      "()V",                    (void *)&JVM_MonitorNotify},
{"notifyAll",   "()V",                    (void *)&JVM_MonitorNotifyAll},
{"clone",       "()Ljava/lang/Object;",   (void *)&JVM_Clone},
};

所以从上面我们知道他们两个方法其实调用的是同一个方法JVM_IHashCode即可,然后再回头看一下之前的代码,我相信你一定会明白了的.

参考:

1.https://www.cnblogs.com/godtrue/p/6395098.html
2.java1.8源码和openjdk源码

你可能感兴趣的:(hashcode 和 System.identityHashCode)