关于System.identityHashCode(obj) 与 obj.hashcode()
看官方提供的API , 对System.identityHashCode()的解释为 :
identityHashCode
public static int identityHashCode(Object x)
-
返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。null 引用的哈希码为 0。
- 可是真的是这样吗???
-
- 让我们做下面的测试:
-
1 public class Main { 2 public static void main(String[] argv) throws Exception { 3 // File file1 = new File("a"); 4 // File file2 = new File("a"); 5 // File file3 = new File("b"); 6 7 String file1 = "abc"; 8 String file2 = "abc"; 9 String file3 = "abcdef";10 11 12 int hc1 = file1.hashCode();13 System.out.println(hc1);14 int hc2 = file2.hashCode();15 System.out.println(hc2);16 int hc3 = file3.hashCode();17 System.out.println(hc3);18 19 int ihc1 = System.identityHashCode(file1);20 System.out.println(ihc1);21 int ihc2 = System.identityHashCode(file2);22 System.out.println(ihc2);23 int ihc3 = System.identityHashCode(file3);24 System.out.println(ihc3);25 }26 }
- 不管注释掉File类 还是 String 类, 运行后的结果都是: hashcode() 与 System.identityHashCode() 生成的数字并不相等~~
- 重新把File类或 String 类换成 Object 时, hashcode() 与 System.identityHashCode() 生成的数字才是相等的 .
- 暂时还还早不到其中的原因, 向各位兄台讨教~
转载于:https://www.cnblogs.com/danne823/archive/2011/11/29/about_hashcode.html
关于System.identityHashCode(obj) 与 obj.hashcode()相关推荐
- Java:Object.hashCode()和System.identityHashCode()的区别
今天在看HashMap源码的时候注意到了这件事:Java源码详解二:HashMap源码分析–openjdk java 11源码 对红黑树中的节点进行排序时,如果hashCode相同,类不是compar ...
- 深入分析Java中打印对象内存地址 System.identityHashCode()方法
文章目录 地址是什么样子的 toString()方法 重写hashCode identityHashCode与真正的内存地址 参考 地址是什么样子的 我们知道system.out.println(ne ...
- obj[]与obj._Ruby中带有示例的Array.rassoc(obj)方法
obj[]与obj. Array.rassoc(obj)方法 (Array.rassoc(obj) Method) In this article, we will study about Array ...
- obj[]与obj._Ruby中带有示例的Array.include?(obj)方法
obj[]与obj. Ruby Array.include?(obj)方法 (Ruby Array.include?(obj) Method) In the previous articles, we ...
- 什么是obj文件?obj文件详解
什么是obj文件?obj文件详解 一.什么是obj文件 OBJ文件是Alias|Wavefront公司为它的一套基于工作站的3D建模和动画软件"Advanced Visualizer&quo ...
- java 中 null==obj 与 obj==null
之前上课,老师推荐将常量放前面进行判断,可以避免空指针的问题.忘记是以null == obj 还是 equals()方法进行说明了.下面进行实践复习. 一. null==obj 与 obj ==nul ...
- java equals重写原则_java中为何重写equals时必须重写hashCode方法详解
前言 大家都知道,equals和hashcode是java.lang.Object类的两个重要的方法,在实际应用中常常需要重写这两个方法,但至于为什么重写这两个方法很多人都搞不明白. 在上一篇博文Ja ...
- Java基础之深入认识hashCode和equals
Java是很多开发者都曾经接触过的一门开发语言,它之所以流行,那是因为他有很多的设计思想解决了很多现实中的问题,其中对象的equals方法,hashCode方法的设计思想值得我们学习,所以我们有必要去 ...
- 重写 hashcode()真有那么简单嘛?
万万没想到一个 hashcode() 方法,既然会引出一堆的知识盲区,简直了. 起因: 老八股:为什么重写Equals方法要重写HashCode方法. 大声告诉我为什么,闭着眼睛先把答案背出来,啥?这 ...
- Java Object有哪些公用方法?
protected Object clone() 创建并返回此对象的一个副本. boolean equals(Object obj) 指示某个其他对象是否与此对象"相等". pro ...
最新文章
- 5.数据绑定和表单标签库
- 微软技术能力测试工具V0.1试用
- 第五十三篇、OC利用AFN上传视频到服务器
- Ubuntu安装中文语言包
- 大数据挑战赛(大佬篇)
- Linux 可执行文件 ELF结构 及程序加载运行
- Android LBS系列05 位置策略(一)
- 动画制作c语言程序,C语言动画制作
- 4 计数器verilog与Systemverilog编码
- Servlet-Response直接返回一个文件并且在浏览器上显示
- 【协同任务】基于matlab多无人机目标搜索与围捕【含Matlab源码 1205期】
- 一份超详细的VTD安装指南(内容含如何安装ubuntu系统及如何在ubuntu中安装显卡驱动)
- GB28181协议常见几种信令流程(一)
- 计算机配件对比,基本参数 尺寸对比 接口对比
- luogu 2411 白银莲花池 luogu 1606 Lilypad Pond
- FSL 功能磁共振影像分析: single-session
- 2020北大软微复试知识整理
- java E201_01_05鬼谷算题
- 天润融通网络电话配置手册
- 数据库设计之学生选课系统_转载