Java-se-interview (1)-string

Source: Internet
Author: User

http://blog.csdn.net/zhangerqing/article/details/8093919

The difference between hashcode and Identityhashcode

The I, Hashcode () method is a method under the object class that is overridden by the inheriting class, computes the hash value based on the object memory address, the string class overrides the Hashcode method, and instead computes the hash value based on the sequence of characters

The II, Identityhashcode () method is a static method in the system class that computes the hash value based on the object memory address;

Method Example:

     Public Static voidMain (string[] args) {//S1 and S2 are two different objects in the following programString S1 =NewString ("Hello"); String S2=NewString ("Hello"); //The string class overrides the Hashcode method of the object class--instead calculates the Hashcode value based on the character sequence .//because S1 and S2 have the same sequence of characters, their Hashcode method return values are the sameSystem.out.println (S1.hashcode () + "----" +S2.hashcode ()); //S1 and S2 are different string objects, so their identityhashcode values are different,//Identityhashcode is calculated based on the address of the object, so any two different objects//Identityhashcode values are always unequalSystem.out.println (System.identityhashcode (S1) + "----" +system.identityhashcode (S2)); //S3 and S4 are the same string objects, so their identityhashcode values are the sameString s3 = "Java"; String S4= "Java"; System.out.println (System.identityhashcode (S3)+ "----"+System.identityhashcode (S4)); }

Output:

Java-se-interview (1)-string

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.