【ジャワ】ハッシュコード()和System.identityHashCode()

ハッシュコード()和System.identityHashCode()

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

最近見ているSpring過程で、このような線光源を見て

@ {リンクorg.springframework.context.support.AbstractApplicationContext}

public AbstractApplicationContext() {
        this.logger = LogFactory.getLog(this.getClass());
        this.id = ObjectUtils.identityToString(this);
        this.displayName = ObjectUtils.identityToString(this);
        this.beanFactoryPostProcessors = new ArrayList();
        this.active = new AtomicBoolean();
        this.closed = new AtomicBoolean();
        this.startupShutdownMonitor = new Object();
        this.applicationListeners = new LinkedHashSet();
        this.resourcePatternResolver = this.getResourcePatternResolver();
}

初期設定でContext設定idし、displayName時間の名前ObjectUtils.identityToString(this)

    public static String identityToString(Object obj) {
        return obj == null ? "" : obj.getClass().getName() + "@" + getIdentityHexString(obj);
    }

    public static String getIdentityHexString(Object obj) {
        return Integer.toHexString(System.identityHashCode(obj));
    }

私たちは見ることができるSpringのアプローチは次のとおりです。类名 + @ + 16进制的字符串

だから、System.identityHashCode()何ですか?

ハッシュコード()とSystem.identityHashCode()比較

例を見てください

public class OK {
    public static void main(String[] args) {
        OK ok1 = new OK();
        OK ok2 = new OK();

        System.out.println("ok1 - hashCode : " + ok1.hashCode());// ok1 - hashCode : 1554874502
        System.out.println("ok2 - hashCode : " + ok2.hashCode());// ok2 - hashCode : 1846274136


        System.out.println("ok1 - System.identityHashCode : " + System.identityHashCode(ok1)); //ok1 - System.identityHashCode : 1554874502
        System.out.println("ok2 - System.identityHashCode : " + System.identityHashCode(ok2));//ok2 - System.identityHashCode : 1846274136
    }
}

ビュー結果の点から、ハッシュコード()とSystem.identityHashCode()が同じオブジェクトと一致しています

次に、我々は(ハッシュコードの下カバー)

public class OK {

    @Override
    public int hashCode() {
        return 1;
    }

    public int getSuperHashCode(){
        return super.hashCode();
    }

    public static void main(String[] args) {
        OK ok1 = new OK();
        OK ok2 = new OK();

        System.out.println("ok1 - hashCode : " + ok1.hashCode()); // ok1 - hashCode : 1
        System.out.println("ok2 - hashCode : " + ok2.hashCode()); // ok2 - hashCode : 1


        System.out.println("ok1 - System.identityHashCode : " + System.identityHashCode(ok1));//ok1 - System.identityHashCode : 1554874502
        System.out.println("ok2 - System.identityHashCode : " + System.identityHashCode(ok2));//ok2 - System.identityHashCode : 1846274136

        System.out.println("ok1 - SuperHashCode : " + ok1.getSuperHashCode());//ok1 - SuperHashCode : 1554874502
        System.out.println("ok2 - SuperHashCode : " + ok2.getSuperHashCode());//ok2 - SuperHashCode : 1846274136


    }
}

オーバーロードされたかどうかを確認することができhashCode()た方法を、しかし前にウォンをオーバーロードしたくなかったobject.hashCode()あなたが使用することができ、System.identityHashCode()

デプスSystem.identityHashCode()

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

System.identityHashCode()、このような文があります

 /**
     * 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);

できる基準源の解釈のためのハッシュコードとどのように底が生成されるidentityHashCode

おすすめ

転載: www.cnblogs.com/xcmelody/p/10961111.html