Share the joy
I tried below code:
int[] a1 = {1, 2}; int[] a2 = {1, 2}; System.out.println(a1.hashCode()); System.out.println(a2.hashCode()); System.out.println(Arrays.hashCode(a1)); System.out.println(Arrays.hashCode(a2));
The result is:
1356652206 1419746043 994 994
The reason is because hashCode() int[] is not overwritten. It is treated as an object. Read below explanation from javaworld, we know hashCode() for Object is a mapping to memory.
Basically the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. If look into the source of Object class , you will find the following code for the hashCode.