Learning experience: How to judge whether the two objects are equal in Java

xiaoxiao2021-03-06  46

Today, the book was collected, and the results found that the fell out of a note, it turned out how to learn Java at the beginning.

A summary of judging if the two objects are equal.

We know, the java.object.equals method can determine if the two objects point to the same memory space, but not

For use, so we need to overload the Equals method to achieve more meaningful comparison ...

The four steps I have summarized are.

1. Is it a comparative object to itself .....

IF (this == OtherObject) Return Ture.

2. Determine whether the comparison object is empty.

IF (OtherObject == Null) Return False.

3. Determine whether the object is inherited in the same parent class.

IF (getClass ()! = OtherObject.getClass ()) Return False;

4. If it is not established, it can be obtained with the same parent class with the current object. Therefore, it starts to compare all its value domains.

All equally two objects, etc.

转载请注明原文地址:https://www.9cbs.com/read-72363.html

New Post(0)