Document directory
Refer to the official API and explain System. identityHashCode () as follows:
IdentityHashCode
public static int identityHashCode(Object x)
-
Returns the hash code of the given object. The Code is the same as the Code returned by the default method hashCode (), regardless of whether the class of the given object overwrites hashCode (). The hash code referenced by null is 0.
-
But is that true ???
-
-
Let's perform the following test:
-
1 public class Main {
2 public static void main(String[] argv) throws Exception {
3 // File file1 = new File("a");
4 // File file2 = new File("a");
5 // File file3 = new File("b");
6
7 String file1 = "abc";
8 String file2 = "abc";
9 String file3 = "abcdef";
10
11
12 int hc1 = file1.hashCode();
13 System.out.println(hc1);
14 int hc2 = file2.hashCode();
15 System.out.println(hc2);
16 int hc3 = file3.hashCode();
17 System.out.println(hc3);
18
19 int ihc1 = System.identityHashCode(file1);
20 System.out.println(ihc1);
21 int ihc2 = System.identityHashCode(file2);
22 System.out.println(ihc2);
23 int ihc3 = System.identityHashCode(file3);
24 System.out.println(ihc3);
25 }
26 }
-
No matter whether the File class or String class is commented out, the running result is: The numbers generated by hashcode () and System. identityHashCode () are not the same ~~
-
When you replace the File class or String class with an Object, the numbers generated by hashcode () and System. identityHashCode () are equal.
-
I am still unable to figure out the reason for this. I 'd like to ask you for advice from the Technical Support Team ~