Categories
RECIPE

The most easy to understand hashCode explanation in history

hashCode() is very useful in collections. Later, the editor will write articles about collections. 해시게임Welcome friends who like to subscribe and pay attention! Well, let’s take a look at the specific content of hashcode().

public int hashCode(): hashCode is a method in the root class Object. By default, hashCode() in Object returns the 32-bit JVM memory address of the object. That is to say, if the object does not override this method, 32 of the corresponding object is returned as the JVM memory address. And is a hash code of type int. The hash code of the object is to better support the Java collection classes based on the hash mechanism, such as Hashtable, HashMap, HashSet, etc.

First, let’s take a look at the hashCode() method in the Object class. After all, the Object is the father of all kinds.

The above is what the Chinese version of the API said. In fact, in the Object source code, the hashCode() method is public native int hashCode(); native means that this method needs to use methods written in other languages ​​to realize its function. I won’t say much here, I don’t know if I can Baidu by myself.

Regarding the hashCode method, the consistent convention is:

1. Objects that override the equals method must also override the hashCode() method.

2. If the two objects return true after being called by equals, then the hashCode method of the two objects must also return the same int hash code.

3. If two objects return false through equals, the value returned by their hashCode is allowed to be the same. (However, the programmer must be aware that hashCode returns a unique hash code that will make the hashtables that store this object work better.

Here’s a sentence about equals, as a reply to the previous question, we know through the analysis of the source code:

(1) The equals in the String class first compares the addresses. If it is a reference to the same object, it is known that the objects are equal and return true.

(2) If it is not the same object, the equals method compares the characters in the two string objects one by one, and returns true only if they are completely equal, otherwise returns false.

Let’s put equals and hashCode together again for a brief summary:

(1) Binding. When the equals method is overridden, it is often necessary to override the hashCode method in order to maintain the general contract of the hashCode method, which states that equal objects must have equal hash codes.

(2) Binding reasons. Hashtable implements a hash table, and in order to successfully store and retrieve objects in a hash table, the objects used as keys must implement the hashCode method and the equals method. Same as (1), objects with equals must be guaranteed to have equal hashCode. Because the hash table retrieves objects by hashCode.

(3) Default.

== By default compares the address of the object in the JVM.

hashCode returns the storage address of the object in the JVM by default.

equal compares the object, the default is also the address of the comparison object in the JVM