Object 方法的 hashCode,equals方法源码
hashCode方法注释
Object 的 hashCode 方法,是本地方法;
Returns a hash code value for the object. This method is
* supported for the benefit of hash tables such as those provided by
* {@link java.util.HashMap}.
注释如是说:提供此方法支持的原因是,为了支持一些 hash tables ;
Whenever it is invoked on the same object more than once during
* an execution of a Java application, the {@code hashCode} method
* must consistently return the same integer, provided no information
* used in {@code 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.
在不修改 equals 方法的前提下,在一次程序执行期间,多次调用同一个对象的 hashCode 方法,得到的结果始终应该是一个相同的整数(Integer 类型);当然,如果在不同的程序中,此结论不成立;
If two objects are equal according to the {@code equals(Object)}
* method, then calling the {@code hashCode} method on each of
* the two objects must produce the same integer result.
如果两个对象的 equals 方法返回值一样,则调用它们各自的 hashCode 方法,返回值也必须保证一样;
It is not required that if two objects are unequal
* according to the {@link java.lang.Object#equals(java.lang.Object)}
* method, then calling the {@code 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.
对于 equals 方法返回值不同的对象,对其 hashCode 的返回值没有必须不同的要求,但是我们应该知道,对于 equals 方法返回值不同的对象,其 hashCode 方法最好也设计成返回不同的结果,这样有助于提高 hash tables 的性能 ;
Object 对象的 hashCode 方法,不同的对象在调用的时候,确实返回了不同的结果,得益于本地实现,返回了对象的内存地址 ;
equals 方法注释
It is reflexive: for any non-null reference value
* {@code x}, {@code x.equals(x)} should return
* {@code true}.
equals 方法具有 自反性,对于非 null 引用 x, x.equals(x) 应该返回 true ;
It is symmetric: for any non-null reference values
* {@code x} and {@code y}, {@code x.equals(y)}
* should return {@code true} if and only if
* {@code y.equals(x)} returns {@code true}.
x
equals 方法具有 对称性,对于非 null 引用 x,y, x.equals(y) 的返回值应该和 y.equals(x) 的返回值保持一致;
It is transitive: for any non-null reference values
* {@code x}, {@code y}, and {@code z}, if
* {@code x.equals(y)} returns {@code true} and
* {@code y.equals(z)} returns {@code true}, then
* {@code x.equals(z)} should return {@code true}.
equals 方法具有 传递性,对于非 null 引用 x,y,z ,如果 x.equals(y) 返回 true , y.equals(z) 返回值为 true,则 x.equals(z) 也必须返回 true;
It is consistent: for any non-null reference values
{@code x} and {@code y}, multiple invocations of
{@code x.equals(y)} consistently return {@code true}
or consistently return {@code false}, provided no
information used in {@code equals} comparisons on the
objects is modified.
equals 方法具有 一致性,对于非 null 引用 x,y ,在没有修改 equals方法的前提下,多次调用 x.equals(y) 的返回值,应该和多次调用 y.equals(z) 的返回值保持一致;
For any non-null reference value {@code x},
* {@code x.equals(null)} should return {@code false}
对于任何的非 null 引用 x ,x.equals(null) 返回 false ;
Note that it is generally necessary to override the {@code hashCode}
method whenever this method is overridden, so as to maintain the
general contract for the {@code hashCode} method, which states
that equal objects must have equal hash codes.
记住了!我们在覆写 equals 方法的时候,最好,也同时覆写 hashCode ,以便保证 equals 方法返回值相同的对象,hashCode方法返回值也相同 ;
equals 方法
public boolean equals(Object obj) {
return (this == obj);
}
Object 类的 equals 方法,内部只是简单的判断对象的地址 ;
- List item
Object 方法的 hashCode,equals方法源码的更多相关文章
- 重写Object类中的equals方法
Object是所有类的父亲,这个类有很多方法,我们都可以直接调用,但有些方法并不适合,例如下面的student类 public class Student { //姓名.学号.年纪 private S ...
- Java学习-025-类名或方法名应用之一 -- 调试源码
上文讲述了如何获取类名和方法名,敬请参阅: Java学习-024-获取当前类名或方法名二三文 . 通常在应用开发中,调试或查看是哪个文件中的方法调用了当前文件的此方法,因而在实际的应用中需要获取相应的 ...
- Object 类中的 equals方法
1 相等与同一 如果两个对象具有相同的类型以及相同的属性值,则称这两个对象相等.如果两个引用对象指的是同一个对像,则称这两个变量同一.Object类中定义的equals 函数原型为:public bo ...
- Java Object类中的equals方法
Object类中的equals方法用于检测一个对象是否等于另外一个对象.在Object类中,这个方法将判断两个对象是否具有相同的引用.如果两个对象具有相同的引用,它们一定是相等的.从这点上看,将其作为 ...
- 关于覆盖Object中的hashCode, equals和toString
最近在看<Effective Java>,里面看到了关于重载hashCode.equals和toString方法的篇章,顿时觉得视野开拓了不少,而且正结合自己工作.项目中的实例,觉得有必要 ...
- 关于Java中hashCode方法的实现源码
首先来看一下String中hashCode方法的实现源码. public int hashCode() { int h = hash; if (h == 0 && value.leng ...
- jQuery原型方法first,last,eq,slice源码分析
这4个方法中前3个方法很常用大家都见过,但是slice方法可能会以为是数组方法,其实slice也是jQuery的一个原型方法,只不过是底层方法是为其他方法服务的(更具体点是为eq方法服务的),首先还是 ...
- HashSet——add remove contains方法底层代码分析(hashCode equals 方法的重写)
引言:我们都知道HashSet这个类有add remove contains方法,但是我们要深刻理解到底是怎么判断它是否重复加入了,什么时候才移除,什么时候才算是包括????????? add ...
- 框架源码系列五:学习源码的方法(学习源码的目的、 学习源码的方法、Eclipse里面查看源码的常用快捷键和方法)
一. 学习源码的目的 1. 为了扩展和调优:掌握框架的工作流程和原理 2. 为了提升自己的编程技能:学习他人的设计思想.编程技巧 二. 学习源码的方法 方法一: 1)掌握研究的对象和研究对象的核心概念 ...
- Redrain 通用菜单控件使用方法和说明(附源码和demo)
转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42889709 大概半年前我写过博客说明怎么改造duilib的原代Menu ...
随机推荐
- OpenFOAM-圆柱绕流
原版视频下载地址:https://yunpan.cn/c64yrdt9J5LmQ 访问密码 0128 首先进行建模操作,任何建模软件均可,本教程采用ICEM直接建模,模型尺寸如下: 建成的模型如下: ...
- 微软校园招聘 研发工程师A
1.const A. const int a; B. int const a; a是常数 C. int const *a; D. const int *a; 常量指针,指向一个常量的指针 E. int ...
- python oracle 写文件 多个SQL变量问题
- hive匹配中文
select regexp_extract('ab中文123测试55..', '[\u4e00-\u9fa5]+', 0) 只提出成功第一段中文汉字,结果为: 中文 select regexp_rep ...
- ActiveMQ持久化
ActiveMQ中,持久化是值对消息数据的持久化.在ActiveMQ中,默认的消息是保存在内存中的.当内存容量不足的时候,或ActiveMQ正常关闭的时候,会将内存中的未处理的消息持久化到磁盘中.具体 ...
- Andorid SQLite数据库开发基础教程(2)
Andorid SQLite数据库开发基础教程(2) 数据库生成方式 数据库的生成有两种方式,一种是使用数据库管理工具生成的数据库,我们将此类数据库称为预设数据库,另一种是使用代码生成的数据库.
- git merge 结果是 git merge Already up-to-date. 该怎么解决?
git将主干合并到当前分支时,出现如下结果: 原因在于:执行git merge前,主干的代码没有更新 正确的操作步骤如下: 1 .切换到主干 $ git checkout master 2. 更新主干 ...
- Spring cloud微服务安全实战-6-4权限控制改造
授权,权限的控制 令牌里的scope包含fly就有权限访问.根据Oauth的scope来做权限控制, 要让@PreAuthorize生效,就要在启动类里面写一个注解. 里面有一个属性叫做,就是在方法的 ...
- C#关于时间(获取特定格式的时间及多种方式获取当前时间戳)以及10位和13位时间戳转为特定格式
C#关于时间(获取特定格式的时间及多种方式获取当前时间戳)以及10位和13位时间戳转为特定格式 置顶 2018年03月06日 19:16:51 黎筱曦 阅读数:19098 标签: C#时间 更多 个人 ...
- Android开发之高仿微信图片选择器
记得刚开始做Andriod项目那会,经常会碰到一些上传图片的功能需求,特别是社交类的app,比如用户头像,说说配图,商品配图等功能都需要让我们到系统相册去选取图片,但官方却没有提供可以选取多张图片的相 ...