【0】README

0.1)以下内容均为原创,包括源代码, 旨在理清 equals 和 hashCode 方法的 实现原理
0.2) for full resource code, please visit
https://github.com/pacosonTang/core-java-volume/blob/master/chapter13/EqualsHashCodeTest.java

【1】equals 方法

1.1)父类Object中的equals 方法, 它本身比较的就是两个对象的内存地址是否相等(Object.equals 源码):

1.2)自定义类中,如果希望对象中各个成员变量 分别相等,则它们的equals 方法 返回 true, 则:


【2】hashCode 方法(String.hashCode 源码)


2.1)而 Object.hashCode 的返回值:
2.2)以下内容转自 API:http://docs.oracle.com/javase/8/docs/api/

The general contract of hashCode is:
Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.)
  • 2.2.1)译文如下:
  • hashCode的常规约定是:
    • 1)对同一个对象的不止一次调用, hashCode 方法必须返回同一个整型值,假如 在equals方法 比较两个对象的过程中 没有信息被修改的话。当对同一个应用的不同执行,这个值(hashCode)不需要保持一致;可以不一样;
    • 2)如果两个对象在 equals 方法中被认为相等, 则 上述两个对象的 hashCode 也应该返回相同的值;
    • 3)不做要求的是,如果 java.lang.Object.equals 认为两个对象不相等,则 两个对象的 hashCode 方法 返回 不同的值。然而, 编码者应该注意的是,对不相等对象产生的不同整型值(hashCode) 可能会影响 哈希表的性能;
  • Object.hashCode 的返回值:(对象的内部地址值)
    • 要做到尽可能合理实用, Object.hashCode 方法对不同的对象产生不同的整型值。(这往往是通过 将对象的内部地址值转换为 整型值来实现的, 但这个实现技术不是 Java 编程语言硬性要求的)。

2.3) 我们再来看 Object.hashCode 的源码:

for detailed native keyword , please visit http://blog.csdn.net/pacosonswjtu/article/details/50312333

java中的equals方法+hashCode方法相关推荐

  1. java baseentity_如何在JPA的BaseEntity中实现equals()和hashcode()方法?

    我有一个BaseEntity类,它是我的应用程序中所有JPA实体的超类. @MappedSuperclass public abstract class BaseEntity implements S ...

  2. java == hashcode,java中==和equals和hashCode的区别

    java中==和equals和hashCode的区别 == 的作用: 基本类型:比较的就是值是否相同 引用类型:比较的就是地址值是否相同(确切的说,是堆内存地址) equals 的作用: 引用类型:默 ...

  3. java中的equals和hashCode

    Java的每个类都继承于Object类.它使用equals()及hashCode()这两个方法来判断两个Object是否相等. 1.  equals() 需要满足5点: 1 自省:对于任一非null引 ...

  4. 面试必问一:Java 中 == 和 equals 的区别你知道吗

    面试必问一:Java 中 == 和 equals 的区别你知道吗 前言 关于这个问题,一般初中级面试中都会遇到,还记得我当初实习找工作的时候也遇到了这个问题,现在都还记得自己是怎么回答的:== 是基本 ...

  5. JAVA中重写equals()方法的同时要重写hashcode()方法

    object对象中的 public boolean equals(Object obj),对于任何非空引用值 x 和 y,当且仅当 x 和 y 引用同一个对象时,此方法才返回 true:注意:当此方法 ...

  6. Java基础提升篇:equals()与hashCode()方法详解

    概述 java.lang.Object类中有两个非常重要的方法: public boolean equals(Object obj) public int hashCode() Object类是类继承 ...

  7. 深入理解Java的equals和hashCode方法

    1.谈谈equals方法 相信大家对这个这个方法一定不陌生.该方法是Object基类里的非final方法(被设计成可覆盖的),下面我们来看看Object中是如何实现该方法的.源代码如下: public ...

  8. Java中重写equals()方法时注意点

    Java中重写equals()方法时注意点 一直说,重写一个对象的equals()方法时我们必须重写HashCode()方法,但是如果我们不重写呢?会有什么影响呢? 首先看一下,什么情况下我们需要重写 ...

  9. java中equals函数所在的类,重写Java中的equals方法介绍

    Java中,只有8种基本类型不是对象,例如:4种整形类型(byte, short, int,long),2种浮点类型(flout, double),boolean, char不是对象,其他的所有类型, ...

最新文章

  1. 应用设计模式进行重构来消除坏味道
  2. Confluence 6 管理协同编辑
  3. MongDB与Spring整合及操作
  4. Android的AutoCompleteTextView在API17高版本添加的setText函数在低版本系统居然能正常调用?官方文档是不是不靠谱了?...
  5. python训练营微信公众号真实性_用python进行微信公众号开发(仅测试学习)
  6. acm第二节2020.4.2-4.4补
  7. VMware多虚拟机网络配置
  8. Sql Server之旅——第十站 简单说说sqlserver的执行计划
  9. Hades:移动端静态分析框架
  10. Python 两个list获取交集,并集,差集的方法(合并、交叉)
  11. Redis——史上最强【集群】入门实践教程
  12. Windows平台下安装证书文件cer的步骤
  13. 3D脚本 maxscript入门教程(7)
  14. autojs识别数字ocr
  15. 115-RTKLIB及GAMP中PPP的随机模型
  16. DataInputStream和DataOutputStream
  17. 基于C语言设计的仓库管理系统(小超市)
  18. 网狐棋牌游戏服务端架构设计原理
  19. border 属性样式
  20. 风暴数码论坛教程-JAVA环境配置

热门文章

  1. 剑指 Offer 43. 1~n 整数中 1 出现的次数(数位dp)
  2. CF848C Goodbye Souvenir
  3. NOIP2021:游记
  4. P1975-[国家集训队]排队【树状数组套线段树】
  5. 【结论】只不过是长的领带(luogu 6877)
  6. [XSY3381] 踢罐子(几何)
  7. Hadoop生态hive(六)Hive QL表
  8. mybatis example处理and、or关系的方法
  9. jQuery Raty星级评分插件使用方法
  10. Oracle入门(十二C)之表修改