preface

To see is to surrender, which is to say that if you can see something, you can get it.

To me recording is seeing.

The interview questions are not just for the interview, but to help us understand what we already know more deeply.

= =

There are two cases

  • If the data types before and after == are basic data types, such as int, long, etc., == compares their values
  • If the data type before and after == is a reference data type, == compares the memory address of the object to which the reference refers

The role of the equals ()

The equals() method is clearly in the base class Object, including the hashCode() method, which means that whenever we create a new class, even if it has nothing in it, It will also have equals() and hashCode() methods, since all classes inherit from Object.

Let’s talk about the equals() method, which is supposed to compare the contents of two objects. But implementation of this method in the Object class = = is used to contrast the two objects, that is to say, if we create a class, not override the equals () method, the call is the Object of the equals () method, then the results of the comparison and the effect of using = = is the same, are compared to the memory address of the Object.

So equals() works in two ways

  • Equals () is the same as using == without overwriting equals()
  • When rewriting equals(), the result of the comparison depends on how it is implemented.

digression

Override equals() in three steps: 1. Use == to determine equality (equality means references refer to the same memory address); 2. Check whether the types are consistent (if the types are inconsistent, the two objects must not be equal); 3. Check whether the attributes of objects are equal based on the actual situation.

Overwriting equals() should also override hashCode() if necessary (I’ll explain why later). In addition, overwriting equals() should satisfy the following criteria:

1. Reflexivity, X. equals(x)

2. Symmetry, X.exals (y), Y.exals (x)

3. Transitivity, X.quals (y), Y.quals (z), X.quals (z)

4. Consistency. No matter how many times the program is run, the result of X. als(y) is always the same

5. Any object that does not equal null calls equals(null) to false

The role of hashCode ()

The hashCode() method evaluates the hash value of an object and returns an int.

So what does it do? Or under what circumstances should this approach be used? See the reference link at the bottom of this article for this question

For a quick explanation, the hashCode method is usually only used when using a hash table, so where is it used? In general, our collections, such as HashMap, Hashtable, HashSet, etc., will use hash tables.

Equals () == equals(

As stated above, equals() is only as good as using == if you don’t override it

Equals () and hashCode()

Looking at our questions above, equals() and hashCode() don’t seem to have any connection. In fact, there’s only one case where they can be used together, and that’s when we create a class that’s going to be used in a HashMap, Hashtable, HashSet, etc., and we have to override both methods at the same time. That’s the connection (see the reference link at the bottom of this article). Let me explain my understanding below.

Suppose our requirement now is to put the class we created into a HashSet. A HashSet collection is not allowed to have identical elements. If we create A class that overwrites only equals(), then we call the equals() and hashCode() methods to compare whether A and B are equal to each other when we add them to the HashSet. If the equals() of A and B return true, then the equals() of B and A return true. Since we haven’t overridden hashCode(), the hashCode() values for A and B are different, and the HashSet will assume that our A and B are different elements, so both our A and B will be added. But in fact, what we see as A and B are equal (because equals() returns true) and should not be put into the HashSet at the same time. This is the paradox, so we need hashCode() when overriding equals() to avoid unpredictable errors.

Here’s a reference to equals() and hashCode() :

In this case, the class’s “hashCode() and equals()” are related:

If two objects are equal, they must have the same hashCode() value.

Equality here means that equals() returns true when comparing two objects.Copy the code

2) If two objects hashCode() are equal, they are not necessarily equal.

Because in a hash table, hashCode() is equal, that is, the hashes of two key-value pairs are equal. However, if hash values are equal, it does not necessarily lead to key-value pairs being equal. To add: "Two different key-value pairs hash equally", this is hash collision.Copy the code

reference

www.jianshu.com/p/5a7f5f786…