Both System.identityHashCode()
and hashCode()
are equivalent in that they return a unique value for an object. But there are differences.
System.identityHashCode()
System.identityHashCode()
is a method that returns the unique hashcode of an object.
The hashcode for the object passed as an argument is returned as an int.
public static native int identityHashCode(Object x)
You can use it like this: In the example below, str1 and str2 have the same hashcode because they point to the same object. But since str3 is a different object, the hashcode is different.
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("str1 hashCode ? " + System.identityHashCode(str1));
System.out.println("str2 hashCode ? " + System.identityHashCode(str2));
System.out.println("str3 hashCode ? " + System.identityHashCode(str3));
Output:
str1 hashCode ? 1789447862
str2 hashCode ? 1789447862
str3 hashCode ? 38997010
You can also compare it to other objects like below.
File file1 = new File("Hello");
String str2 = "Hello";
System.out.println("file1 hashCode ? " + System.identityHashCode(file1));
System.out.println("str2 hashCode ? " + System.identityHashCode(str2));
Output:
file1 hashCode ? 1789447862
str2 hashCode ? 38997010
Return value for null is 0, like System.identityHashCode(null).
hashCode()
hashCode()
is defined in the Object class, which is the parent class of all objects.
public native int hashCode();
And subclasses can override hashCode()
.
That`s why there are cases where different objects but the same hashcode, like this:
String str1 = "Hello";
String str2 = "Hello";
String str3 = new String("Hello");
System.out.println("str1 hashCode ? " + str1.hashCode());
System.out.println("str2 hashCode ? " + str2.hashCode());
System.out.println("str3 hashCode ? " + str3.hashCode());
Output:
str1 hashCode ? 69609650
str2 hashCode ? 69609650
str3 hashCode ? 69609650
Since str1 and str2 are the same object, the hashcode can be the same. But since str2 and str3 are different objects, I expected the hashcode to be different, but they are the same.
The reason is that hashCode()
is overridden as follows in the String class.
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
If you look at the code, you are creating a hashcode with value, a string of String. So even if the object is different, if the string is the same, the same hashcode is returned.
Clean up
Object`s hashCode()
method does not have a unique value for each object because it can be overridden in subclasses.
This method can be overridden to express that the properties of the objects are the same. For example, if the hashcode of a String is, it means that the string is the same even if the objects are different.
On the other hand, System.identityHashCode()
cannot be overridden and returns a unique hashCode of the object.
It is recommended to use this method if you want to compare the objects themselves.
Reference
Related Posts
- Java - Remove items from List while iterating
- Java - How to find key by value in HashMap
- Java - Update the value of a key in HashMap
- Java - How to put quotes in a string
- Java - How to put a comma (,) after every 3 digits
- BiConsumer example in Java 8
- Java 8 - Consumer example
- Java 8 - BinaryOperator example
- Java 8 - BiPredicate Example
- Java 8 - Predicate example
- Java 8 - Convert Stream to List
- Java 8 - BiFunction example
- Java 8 - Function example
- Java - Convert List to Map
- Exception testing in JUnit
- Hamcrest Collections Matcher
- Hamcrest equalTo () Matcher
- AAA pattern of unit test (Arrange/Act/Assert)
- Hamcrest Text Matcher
- Hamcrest Custom Matcher
- Why Junit uses Hamcrest
- Java - ForkJoinPool
- Java - How to use Futures
- Java - Simple HashTable implementation
- Java - Create a file in a specific path
- Java - Mockito의 @Mock, @Spy, @Captor, @InjectMocks
- Java - How to write test code using Mockito
- Java - Synchronized block
- Java - How to decompile a ".class" file into a Java file (jd-cli decompiler)
- Java - How to generate a random number
- Java - Calculate powers, Math.pow()
- Java - Calculate the square root, Math.sqrt()
- Java - How to compare String (==, equals, compare)
- Java - Calculate String Length
- Java - case conversion & comparison insensitive (toUpperCase, toLowerCase, equalsIgnoreCase)