Object的equals方法

1. equals是类的实例方法。

2.方法翻译

public boolean equals(Object obj)
Indicates whether some other object is "equal to" this one.

(句子结构:动宾,宾语由whether引导的名词从句充当。some other + 可数名词单数,译为某一事物。)

指示了某一对象与这个对象是否相等。


The equals method implements an equivalence relation on non-null object references:

equals方法实现了关于非空对象引用的一种等价关系。

  • It is reflexive: for any non-null reference value xx.equals(x) should return true.
  • 它是反身的:对于任何非空引用值x, x.equals(x)应该返回ture。
  • It is symmetric: for any non-null reference values x and yx.equals(y) should return true if and only if y.equals(x) returns true.
  • 它是对称的: 对于任何非空引用值x和y, 当且仅当y.equals(x)返回true时,x.equals(y)应该返回true。
  • It is transitive: for any non-null reference values xy, and z, if x.equals(y) returns true and y.equals(z) returns true, thenx.equals(z) should return true.
  • 它是传递的: 对于任何非空引用值x,y,和z,如果x.equals(y)返回true,y.equals(z)返回true,那么x.equals(z)应该返回true。
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • 它是一致的:对于任何非空引用值x和y,x.equals(y)的多次调用总是返回true或总是返回false,不提供修改使用于对象的equals比较的信息。
  • For any non-null reference value xx.equals(null) should return false.
  • 对于任何非空引用值x,x.equals(null)应该返回false。

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

类Object的equlas方法,实现了关于对象的最有区别的可能等价关系;即,对于任何非空引用值x和y,当且仅当x和y引用同一个对象时(x == y得到值true),这个方法返回true。

Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.

注意,无论何时这个方法被重写,一般需要重写hashCode方法,为了保持与hashCode方法通用一致,这样做就规定了相等的对象必须有相等的散列码。

Parameters: 形参
obj - the reference object with which to compare.参与比较的引用对象。
Returns: 返回
true if this object is the same as the obj argument;  false otherwise.如果这个对象与obj参数相同返回true,否则返回false。


Object类的equals方法是Java中的一个重要方法,用于判断两个对象是否相等。它的默认实现是比较两个对象的引用是否相同,即比较两个对象的内存地址是否相同。但是,我们可以根据需要在自定义类中重写equals方法来定义自己的相等条件。 一般来说,我们在重写equals方法时需要满足以下几个要求: 1. 自反性:对于任何非空引用值x,x.equals(x)应该返回true。 2. 对称性:对于任何非空引用值x和y,如果x.equals(y)返回true,则y.equals(x)也应该返回true。 3. 传递性:对于任何非空引用值x、y和z,如果x.equals(y)返回true,并且y.equals(z)也返回true,则x.equals(z)也应该返回true。 4. 一致性:对于任何非空引用值x和y,多次用x.equals(y)应该始终返回相同的结果,前提是对象上的信息没有被修改。 5. 非空性:对于任何非空引用值x,x.equals(null)应该返回false。 在实际使用中,我们可以根据对象的属性来判断它们是否相等,比如比较两个字符串的内容是否相同,或者比较两个自定义类对象的特定属性是否相等。在重写equals方法时,通常还需要重写hashCode方法,以确保相等的对象具有相同的哈希码。 需要注意的是,equals方法在比较对象时应该使用instanceof关键字进行类型检查,以避免ClassCastException异常的发生。此外,还可以使用Objects类的equals方法来简化equals方法的实现。 这就是关于Object类的equals方法的一些基本介绍。如果你有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值