Object.hashCode()方法与System.identityHashCode(object)的区别
来源:互联网 发布:淘宝卖家寄快递的软件 编辑:程序博客网 时间:2024/04/28 20:37
String a = new String("hhh");
String b = new String("hhh");
System.out.println(System.identityHashCode(a));
System.out.println(System.identityHashCode(b));
System.out.println(a.hashCode());
System.out.println(b.hashCode());
打印结果
前两个不同
后两个相同
这是为什么呢,我们知道目前a和b是两个不同的对象,他们在内存中存放的地址是不同的,System.identityHashCode方法是java根据对象在内存中的地址算出来的一个数值,不同的地址算出来的结果是不一样的。因此这里打印出的结果不一样。
doc上如是说:返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。
但是为什么后两个相同呢?这是因为,String类中已经重新写了hashCode()方法,也就是说,String类中hashcode,已经不是根据对象在内存中的地址计算出来的。(具体怎么算出来的,我还没有研究),就是说即使对象在内存中的地址不一样,String中hashcode也可能一样
0 0
- Object.hashCode()方法与System.identityHashCode(object)的区别
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- hashCode和identityHashCode的区别
- JDK源码学习--System.identityHashCode(Object)
- 关于System.identityHashCode(obj) 与 obj.hashcode()
- UnityEngine.Object与System.Object的联系和区别
- Object下的hashCode方法
- System.gc()与Object.finalize()的区别
- System.gc()与Object.finalize()的区别
- System.gc()与Object.finalize()的区别
- System.gc()与Object.finalize()的区别
- object... 与object[]的区别
- Object中的hashCode()方法
- Java Object.hashCode()方法
- iOS中属性与成员变量的区别
- 第十一题(求二叉树中节点的最大距离)
- IP地址的分类——a,b,c 类是如何划分的
- 超快速理解const修饰的作用
- 02js基础 js基础语法
- Object.hashCode()方法与System.identityHashCode(object)的区别
- SICP 习题 (1.42)解题总结
- C++杂记:堆栈的用法与区别
- shell脚本知识
- 关于Android Bitmap Blending – Color Channels
- win7+vs2010+opencv-2.4.9配置测试教
- TopCoder SRM 625 Incrementing Sequence 题解
- python读写不同编码txt文件
- 使用forever运行nodejs应用