What is the difference between the equivalent comparison operator (==) and equals() methods in Java?

Asked 2 years ago, Updated 2 years ago, 18 views

I'd like to get confirmation that I understand the following exactly.

Is that what I understand?

java

2022-09-21 19:33

1 Answers

In general, the answer to the above question is "Yes". However...

The equals() method compares only those written for comparison. It's nothing more, nothing less.

If you do not override the equals() method in the class, the equals(Objecto) method of the nearest superclass that overrides this method is the default.

If you do not have superflats that override the equals() method, the method Object#equals(Objecto) defined by the object class, which is the top superclass, is the default. It shows the same result as the == operator. That is, returns true only for two variables that refer to the same object. So you want to test the identity of the object and the non-functional identity.

Always remember that to override the equals() method, you must also override the hashCode(). If the equals() method has the same value for both objects, the result returned from hashCode() for both objects must be the same. The opposite is not necessarily true.


2022-09-21 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.