Java double equals sign, Equals (), Hashcode () Summary

Source: Internet
Author: User

By default-double equals = =, the memory address is compared. -Equals (), the memory address is compared by default. -Hashcode (), the default is to return the memory address of object. The way in which the method is rewritten in string often encounters situations where you need to override Equals () and hashcode (), such as a string that rewrites both methods. The implementation of the string Equals () is to first determine whether the memory space of two strings is consistent and then compare each character. The implementation of String Hashcode () is *31 to each character and then accumulates. The contract for Equals () and Hashcode () has a contract in Java:-If two objects are equal, i.e. equals () returns True, their hashcode () must be equal. -but if the hashcode () of two objects is equal, the two objects (judged by Equals ()) are not necessarily equal. For example, when accessing key-value pairs in HashMap, these two methods are used:-Using hashcode () to calculate the position of the entry in the array-the likelihood of collisions, that is, multiple entry hash code is the same, there is the same "lattice"-at this point, you need to use equals () to determine if the key is identical in general, the override of Equals () and hashcode () is bound to occur because the contractual relationship is guaranteed. Reference-http://www.cnblogs.com/xudong-bupt/p/3960177.html-http://www.importnew.com/8701.html

Java double equals sign, Equals (), Hashcode () Summary

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.