hashcode()和System.identityHashCode()

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

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

@{link org.springframework.context.support.AbstractApplicationContext}

  1. public AbstractApplicationContext() {
  2. this.logger = LogFactory.getLog(this.getClass());
  3. this.id = ObjectUtils.identityToString(this);
  4. this.displayName = ObjectUtils.identityToString(this);
  5. this.beanFactoryPostProcessors = new ArrayList();
  6. this.active = new AtomicBoolean();
  7. this.closed = new AtomicBoolean();
  8. this.startupShutdownMonitor = new Object();
  9. this.applicationListeners = new LinkedHashSet();
  10. this.resourcePatternResolver = this.getResourcePatternResolver();
  11. }

在初始化Context时设置 iddisplayName名字的时候 ObjectUtils.identityToString(this)

  1. public static String identityToString(Object obj) {
  2. return obj == null ? "" : obj.getClass().getName() + "@" + getIdentityHexString(obj);
  3. }
  4. public static String getIdentityHexString(Object obj) {
  5. return Integer.toHexString(System.identityHashCode(obj));
  6. }

可以看到Spring的做法是:类名 + @ + 16进制的字符串

所以System.identityHashCode()是什么?

hashcode()和System.identityHashCode()对比

来看个实例

  1. public class OK {
  2. public static void main(String[] args) {
  3. OK ok1 = new OK();
  4. OK ok2 = new OK();
  5. System.out.println("ok1 - hashCode : " + ok1.hashCode());// ok1 - hashCode : 1554874502
  6. System.out.println("ok2 - hashCode : " + ok2.hashCode());// ok2 - hashCode : 1846274136
  7. System.out.println("ok1 - System.identityHashCode : " + System.identityHashCode(ok1)); //ok1 - System.identityHashCode : 1554874502
  8. System.out.println("ok2 - System.identityHashCode : " + System.identityHashCode(ok2));//ok2 - System.identityHashCode : 1846274136
  9. }
  10. }

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

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

  1. public class OK {
  2. @Override
  3. public int hashCode() {
  4. return 1;
  5. }
  6. public int getSuperHashCode(){
  7. return super.hashCode();
  8. }
  9. public static void main(String[] args) {
  10. OK ok1 = new OK();
  11. OK ok2 = new OK();
  12. System.out.println("ok1 - hashCode : " + ok1.hashCode()); // ok1 - hashCode : 1
  13. System.out.println("ok2 - hashCode : " + ok2.hashCode()); // ok2 - hashCode : 1
  14. System.out.println("ok1 - System.identityHashCode : " + System.identityHashCode(ok1));//ok1 - System.identityHashCode : 1554874502
  15. System.out.println("ok2 - System.identityHashCode : " + System.identityHashCode(ok2));//ok2 - System.identityHashCode : 1846274136
  16. System.out.println("ok1 - SuperHashCode : " + ok1.getSuperHashCode());//ok1 - SuperHashCode : 1554874502
  17. System.out.println("ok2 - SuperHashCode : " + ok2.getSuperHashCode());//ok2 - SuperHashCode : 1846274136
  18. }
  19. }

可以看到,如果重载了hashCode()方法,而又想获未重载之前的object.hashCode(),则可以使用System.identityHashCode()

深入System.identityHashCode()

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

关于System.identityHashCode()里面的声明是这样的

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

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

【Java】 hashcode()和System.identityHashCode()的更多相关文章

  1. Java基础教程——System类

    System类 java.lang.System类代表当前Java程序的运行平台. |-可以做输入输出,垃圾回收:(此处不讲) |-可以获取时间: |-可以获取环境变量: |-可以获取系统信息: |- ...

  2. 1.Java基础之System对象

    毕向东老师Java基础学习笔记——System对象 今天学习Java中的System对象后,感觉这个对象对我们主要有以下几点用处. 1.获取当前操作系统版本和类型. 2.获取当前操作系统的path中的 ...

  3. Java 中的System.exit

    在java 中退出程序,经常会使用System.exit(1) 或 System.exit(0). 查看System.exit()方法的源码,如下 /** * Terminates the curre ...

  4. CRC32 vs Java.HashCode

    找了容量为27万中文词库进行试验    CRC32 中冲突率 < 0.01%    而 Java.HashCode 有 4%    hashCode 的速度 应该比 CRC 快 2-3 倍 CR ...

  5. JAVA中的System.in

    System.in读取标准输入设备数据(从标准输入获取数据,一般是键盘),其数据类型为InputStream.方法: int read()   // 返回输入数值的ASCII码,,该值为0到 255范 ...

  6. Exception in thread "main" java.lang.IllegalArgumentException: System memory 202768384 must be at least 4.718592E8. Please use a larger heap size.

    Spark-submit 提交任务时候报错 Exception in thread "main" java.lang.IllegalArgumentException: Syste ...

  7. 对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高不少,并且不受NTP等外部服务影响,能准确更准确来统计耗时(java中对应的是System.nanoTime),也就是说所有使用gettimeofday来统计耗时(java中是System.curre

    对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高 ...

  8. java中的System.copyof()与Array.copyof()区别

    java中的System.copyof()与Array.copyof()区别 在复制数组时我们可以使用System.copyof(),也可以使用Array.copyof(),但是它们之间是有区别的.以 ...

  9. Java基础 之 System.getProperty()方法

    Java基础 之 System.getProperty()方法大全 public static void main(String[] args) { System.out.println(" ...

随机推荐

  1. 【Lintcode】118.Distinct Subsequences

    题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...

  2. 【Lintcode】018.Subsets II

    题目: Given a list of numbers that may has duplicate numbers, return all possible subsets Notice Each ...

  3. RDA项目打包

    注意APP的编译搭建: ./aps/Makefile.toolchain //ccoption path的设定 ./aps/rules.mak //统一的编译规则 MAKE -C 1.TOOLS的可执 ...

  4. HTML DOM nodeType 属性

    实例 获得 body 元素的节点类型: document.body.nodeType; 结果: 1 定义和用法 nodeType 属性返回以数字值返回指定节点的节点类型. 如果节点是元素节点,则 no ...

  5. AR/VR-VR:VR

    ylbtech-AR/VR-VR:VR 虚拟现实技术是一种可以创建和体验虚拟世界的计算机仿真系统,它利用计算机生成一种模拟环境,是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真使用户沉浸 ...

  6. DS:template

    ylbtech-DS: 1.返回顶部   2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部     6.返回顶部   作者:ylbtech出处:http://ylbtech.cnbl ...

  7. redis和memcache的比较

    1.数据类型支持不同 与Memcached仅支持key-value结构不同,Redis支持的数据类型更丰富,同时支持list.set.hash等数据结构的存储: 2.内存管理不同 在Redis中,并不 ...

  8. 在VMWare上安装ubuntu及VMWare&amp;nbs…

    在VMWare上安装ubuntu及VMWare Tools 一.摘要 该文主要介绍了如何在虚拟机上安装ubuntu,和安装VMWare Tools设置共享文件夹,最后对ubuntu做了简单的介绍. 二 ...

  9. 24.集成ASP.NETCore Identity

    正常的情况下view页面的错误的显示应该是这么去判断的 这里我们就不加判断为了,直接用这个div 显示就可以了.当有错误会自动显示在div内 asp.net core Identity加入进来 这里用 ...

  10. utunbu下的codeblocks配置openGL环境

    真想骂娘阿,刚开始用utunbu,什么也不明白,不明白我装都软件都在哪里,不知道就像windows下的系统文件那样的文件在哪里,也不知道如何配置环境变量.就这样稀里糊涂的,还要抓紧时间装openGL, ...