且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用等号比较String和Integer

更新时间: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 an Integer object that contains the same int value as this object.


  • String.equals()


    当且仅当参数不是 null 是a时,结果才为真 S研ng object ,表示与此对象相同的字符序列。

    The result is true if and only if the argument is not null and is a String 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.