1. If the values are different, comparison using == and equals() will return false 2. The values are the same Use == to compare: Basic type – basic type, basic type – wrapped object returns true Packaging object – packaging object, if it is not the same object (the memory address of the object is […]
Tag: equals
“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 […]
Solve the rewriting problem of equals() and hashcode() in one article
Table of Contents Part 1 What does equals() do? Part 2 What is the difference between equals() and ==? Part 3 The role of hashCode() Part 4 The relationship between hashCode() and equals() Part 5: Several classic interview questions Part 1 What does equals() do? The function of equals() is used to determine whether two […]
A brief analysis of what the Obiect class has–equals method, toString method, hashCode method
The Obiect class is a class provided by Java by default. Except for the Obiect class, all classes in Java have an inheritance relationship and inherit the Object class by default. That is, we can think of the Object class as our top-level parent class. For example, let’s give an example to prove that Object […]
== 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: […]
Why should we rewrite hashCode() after rewriting equals()?
Why should we rewrite hashCode() after rewriting equals() Article 9 in Effective Java stipulates that when overriding the equals() method, always override the hashCode() method. Why is this? Next we will introduce these two methods. The equals() method and the hashCode() method in Java are both methods in the Object class, and all classes in […]