How is the == operator implemented in Java?

后端 未结 4 2011
無奈伤痛
無奈伤痛 2021-01-04 23:33

Specifically, in the case of object reference equality, what does the == operator do?

Does the comparison return true if the references evaluate to the same object a

4条回答
  •  执念已碎
    2021-01-05 00:24

    The == operator compares object references to see if they are identical, i.e. they refer to the same object in memory.

    The equals() method compares object references to see if they are equivalent, though not necessarily identical. The default implementation of equals() uses the == operator, but it often makes sense to override this behavior. For example, you might want two BankAccount references to be considered equivalent if they have the same account number, even if they are completely different objects.

提交回复
热议问题