About System. identityHashCode (obj) and obj. hashcode ()

Source: Internet
Author: User
Document directory
  • IdentityHashCode

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.