Let's say that you have overridden an object's equals() and hashCode() methods, so that they use the object's fields.
How you do you check if two references are to the same object, ala the stock equals() method?
When a reference type is compared to a primitive type, the reference types actual value is compared to the value of the primitive type. However when two reference types are compared, the references themselves are compared, not the values.
== should be used during reference comparison. == checks if both references points to same location or not. equals() method should be used for content comparison. equals() method evaluates the content to check the equality.
The equals() tells the equality of two strings whereas the compareTo() method tell how strings are compared lexicographically.
pointOne. equals(pointTwo); Determines whether or not two points are equal. Two instances of Point2D are equal if the values of their x and y member fields, representing their position in the coordinate space, are the same.
Use ==
on objects to perform identity comparison.
That is what the default implementation of equals()
does, but one normally overrides equals()
to serve as an "equivalent content" check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With