• 【Java】 hashcode()和System.identityHashCode()


    hashcode()和System.identityHashCode()

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

    最近在看Spring源码的过程中看到这么一行

    @{link 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时设置 iddisplayName名字的时候 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()是什么?

    hashcode()和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
        }
    }
    

    从结果上来看,相同对象的hashCode()和System.identityHashCode()是一致的

    接下来,我们覆盖下hashCode()

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

    对于源码中的解读可以参考 hashCode和identityHashCode底层是怎么生成的

  • 相关阅读:
    Window10和Ubuntu 18.04双系统安装的引导问题解决
    Linux gcc链接动态库出错:LIBRARY_PATH和LD_LIBRARY_PATH的区别
    PaddleBook的部署安全性问题
    ubuntu docker中crontab任务不执行的问题
    zsh 自动补全导致命令显示重复
    For Freedom —— 代理篇
    搭建自己私有的PKM系统,各家PKM大比拼。。附:构建自己熟悉的基础Docker,破解联通光猫
    Docker上ubuntu新建用户的网络访问不通问题
    Android学习杂记
    jquery中的工具函数 Utilities
  • 原文地址:https://www.cnblogs.com/xcmelody/p/10961111.html
Copyright © 2020-2023  润新知