“Effective Java” Talk: The equals problem caused by inheritance

1. Logical equivalence convention Looking through the Objects source code, you can see that Objects defines the equals() method, which is used to indicate whether other objects are logically equal to this object. The equals method has the following conventions: Reflexivity: x.equals(x) should return true for any non-null reference value x. Symmetry: For any non-null […]

Objects.equals has pitfalls!

When a friend was reviewing someone else’s code recently, he discovered that a colleague used the Objects.equals method to determine whether two values were equal in a certain business scenario, and returned results that were inconsistent with expectations, which aroused my concern. interest of. I originally thought that the judgment result would return true, but […]

[Program Robustness] I heard that you have always loved equals. . .

Think about the final final is used to declare variables/parameters/properties, methods and classes. Modified variables: If the variable is a basic type, its value does not change; if it is an object, the reference cannot be changed (the content is variable). Modified method: The method cannot be overridden (whether it is inheritable or not depends […]

== and equals and hashCode

== “==” is the operator If the comparison object is a basic data type, the comparison is whether the stored values are equal; If the comparison is a reference data type, the comparison is whether the address value of the pointed object is equal (whether it is the same object). public static void main(String[] args) […]

Popular explanation of the relationship and functions of equals and hashCode

Foreword Why do you have to override the hashCode method when overriding the equals method? This is a familiar and basic interview question. As working hours increase, many of the eight-part interview theories are gradually verified in work practice. But there are also some theories. After passing the interview stage, it seems that they will […]

Java hashCode method and equals method

Java’s hashCode method and equals method Brief description public native int hashCode() public boolean equals(Object obj) {<!– –> return (this == obj); } Feature description Returns the hash value of the object. This method is supported for use with hash tables, such as those provided by java.util.HashMap. The general convention of hashCode is as follows: […]