【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时设置 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()是什么?
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()的更多相关文章
- Java基础教程——System类
System类 java.lang.System类代表当前Java程序的运行平台. |-可以做输入输出,垃圾回收:(此处不讲) |-可以获取时间: |-可以获取环境变量: |-可以获取系统信息: |- ...
- 1.Java基础之System对象
毕向东老师Java基础学习笔记——System对象 今天学习Java中的System对象后,感觉这个对象对我们主要有以下几点用处. 1.获取当前操作系统版本和类型. 2.获取当前操作系统的path中的 ...
- Java 中的System.exit
在java 中退出程序,经常会使用System.exit(1) 或 System.exit(0). 查看System.exit()方法的源码,如下 /** * Terminates the curre ...
- CRC32 vs Java.HashCode
找了容量为27万中文词库进行试验 CRC32 中冲突率 < 0.01% 而 Java.HashCode 有 4% hashCode 的速度 应该比 CRC 快 2-3 倍 CR ...
- JAVA中的System.in
System.in读取标准输入设备数据(从标准输入获取数据,一般是键盘),其数据类型为InputStream.方法: int read() // 返回输入数值的ASCII码,,该值为0到 255范 ...
- 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 ...
- 对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高不少,并且不受NTP等外部服务影响,能准确更准确来统计耗时(java中对应的是System.nanoTime),也就是说所有使用gettimeofday来统计耗时(java中是System.curre
对于应用需要记录某个方法耗时的场景,必须使用clock_gettime传入CLOCK_MONOTONIC参数,该参数获得的是自系统开机起单调递增的纳秒级别精度时钟,相比gettimeofday精度提高 ...
- java中的System.copyof()与Array.copyof()区别
java中的System.copyof()与Array.copyof()区别 在复制数组时我们可以使用System.copyof(),也可以使用Array.copyof(),但是它们之间是有区别的.以 ...
- Java基础 之 System.getProperty()方法
Java基础 之 System.getProperty()方法大全 public static void main(String[] args) { System.out.println(" ...
随机推荐
- 记一次keepalived脑裂问题查找
在自己环境做keepalived+Redis实验时,当重启了备用redies机器后,发现两台redies主机都拿到了VIP [root@redis2 ~]# ip addr list 1: lo: & ...
- 查看SELinux状态&关闭SELinux
1. 查看SELinux状态 1.1 getenforce getenforce 命令是单词get(获取)和enforce(执行)连写,可查看selinux状态,与setenforce命令相反. se ...
- POCO库中文编程参考指南(7)Poco::Net::DatagramSocket
1 构造函数 创建一个未连接的 IPv4 数据报 Socket: DatagramSocket(); 创建一个指定 IP 类型(IPv4 或 IPv6)的数据报 Socket: explicit Da ...
- javascript中原型链存在的问题
我们知道使用原型链实现继承是一个goodway:)看个原型链继承的例子. function A () { this.abc = 44; } A.prototype.getAbc = function ...
- 'xxx' declared `static' but never defined
'xxx' declared `static' but never defined [问题描述] uart.c文件中有函数read_sample的实现: [plain] view plain copy ...
- 转:Serializable---序列化
Serializable 今天在看代码的时候,看到[Serializable],不明白是什么意思.查阅了网上的一些资料,才明白这是指给类添加序列化的特性,即添加后它就可以进行序列化,那什 ...
- ABP启动流程分析
添加服务与注册中间件 public IServiceProvider ConfigureServices(IServiceCollection services) { // Configure Abp ...
- Hibernate中注解的开发
转自:https://blog.csdn.net/liujiahan629629/article/details/22335563 在利用注解开发数据库持久层以前,需要学习一个规范JPA(JavaPe ...
- Stored Procedures CASE 用法错误
)) ) select @type=[type] from sys.objects with(nolock) where name=@ObjectName case @typ ...
- Gym - 100801H Hash Code Hacker (构造)
题意:求 n 个哈希值相同的串. 析:直接构造,通过取模来查找相同的串. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400000 ...