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底层是怎么生成的

【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. ACM学习历程——HDU2227 Find the nondecreasing subsequences(线段树 && dp)

    Description How many nondecreasing subsequences can you find in the sequence S = {s1, s2, s3, ...., ...

  2. Linux系统上php-cli安装redis扩展

    下载 假设已经安装了redis-server,现在我们来安装redis扩展. 下载ZIP包: https://github.com/phpredis/phpredis/tree/master . 解压 ...

  3. 用WINHEX合并两个或多个BIN文件

    以前,我给W25Q16下载内容的时候,每次都要分别传输GBK字符.英文字符和图片BIN文件,每次都要传输好几次. 后来,我发现,用WINHEX软件可以把这些BIN文件都合并到一个文件,只需要传输一次就 ...

  4. 解决Visual Code安装中文插件失败问题

    早已听闻Visual Code的大名,今日一用,果然不同凡响. 只不过,我的常用开发环境是不联网的,需要离线安装Visual Code和扩展插件. 以前要安装插件只能从VsCode里装,想离线安装比较 ...

  5. 使用XMLConfiguration解析xml,PropertiesConfiguration解析properties等相应信息

    org.apache.commons.configuration.XMLConfiguration; Apache Common-Configuration工具可以从Properties文件,XML文 ...

  6. 1、R-reshape2-cast

    1.cast:     长型数据转宽型数据 (1).acast,dcast的区别在于输出结果.acast 输出结果为vector/matrix/array,dcast 输出结果为data.frame. ...

  7. 1.4 DVWA亲测文件上传漏洞

    Low 先看看源代码: <?php if(isset( $_POST[ 'Upload' ] ) ) { // Where are we going to be writing to? $tar ...

  8. 滴滴Booster移动APP质量优化框架 学习之旅 三

    推荐阅读: 滴滴Booster移动App质量优化框架-学习之旅 一 Android 模块Api化演练 不一样视角的Glide剖析(一) 滴滴Booster移动App质量优化框架-学习之旅 二对重复资源 ...

  9. Celery 基本使用

    1. 认识 Celery Celery 是一个 基于 Python 开发的分布式异步消息任务队列,可以实现任务异步处理,制定定时任务等. 异步消息队列:执行异步任务时,会返回一个任务 ID 给你,过一 ...

  10. Android图片压缩框架-Tiny 集成

    为了简化对图片压缩的调用,提供最简洁与合理的api压缩逻辑,对于压缩为Bitmap根据屏幕分辨率动态适配最佳大小,对于压缩为File优化底层libjpeg的压缩,整个图片压缩过程全在压缩线程池中异步压 ...