hashCode

For reference to the previous article on hashCode, click reference.

identityHashCode

Java.lang.System#identityHashCode is a native method provided in System.

/**
 * Returns the same hash code for the given object as
 * would be returned by the default method hashCode(),
 * whether or not the given object's class overrides
 * hashCode().
 * The hash code for the null reference is zero.
 *
 * @param x object for which the hashCode is to be calculated
 * @return  the hashCode
 * @since   JDK1.1
 */
public static native int identityHashCode(Object x);
Copy the code

The difference between identityHashCode and hashCode is that identityHashCode returns the hashCode of the object, regardless of whether the object overrides the hashCode method.

The sample

public static void main(String[] args) {
    String str1 = new String("abc");
    String str2 = new String("abc");
    System.out.println("str1 hashCode: " + str1.hashCode());
    System.out.println("str2 hashCode: " + str2.hashCode());
    System.out.println("str1 identityHashCode: " + System.identityHashCode(str1));
    System.out.println("str2 identityHashCode: " + System.identityHashCode(str2));

    User user = new User("test", 1);
    System.out.println("user hashCode: " + user.hashCode());
    System.out.println("user identityHashCode: " + System.identityHashCode(user));
}
Copy the code

Output result:

str1 hashCode: 96354
str2 hashCode: 96354
str1 identityHashCode: 1173230247
str2 identityHashCode: 856419764
user hashCode: 621009875
user identityHashCode: 621009875
Copy the code

Results analysis:

1, str1 and STR2 have the same hashCode because the String class overrides the hashCode method, which determines the value of hashCode based on the value of String, so as long as the value is the same, hashCode will be the same.

Str1 and STR2 have different identityHashCode. Although String overwrites the hashCode method, identityHashCode always returns a hash value based on the physical memory address of the object, so each String has a different physical address. IdentityHashCode will be different.

The User object does not override the hashCode method, so hashCode and identityHashCode return the same value.

conclusion

The hashCode method can be overridden and returns the overridden value. IdentityHashCode returns the hash value of an object regardless of whether the object overrides the hashCode method.

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.