更新时间:2023-12-05 13:34:10
整数
永远不会等于
到字符串
。
两个类都非常严格 equals()
仅接受各自类型对象的定义。
Both classes have very strict equals()
definitions that only accept objects of their respective types.
如果和,结果为true仅当参数不是
null
且是包含相同int的Integer
对象时值作为此对象。
The result is true if and only if the argument is not
null
and is anInteger
object that contains the same int value as this object.
当且仅当参数不是
null
且是a时,结果才为真S研ng
object ,表示与此对象相同的字符序列。
The result is true if and only if the argument is not
null
and is aString
object that represents the same sequence of characters as this object.
这实际上是实现 equals()
的一种非常常见的方式:只有同一类的对象(有时是子类)才能平等。其他实现是可能的,但也是例外。
That's actually a quite common way to implement equals()
: only objects of the same class (and occasionally subclasses) can be equal. Other implementations are possible, but are the exception.
一个常见的例外是集合例如 List
:每
遵循约定的实现将与任何其他实现进行比较时返回 true
,如果它具有相同的内容以相同的顺序。
One common exception are the collections such as List
: every List
implementation that follows the convention will return true
when compared to any other implementation, if it has the same content in the same order.