The difference between = =, equals, and Hashcode in Java

Source: Internet
Author: User

in Java, = =, Equals (), and hashcode () are all related to the comparison of objects.
  1. about = =

    = = is easy to understand. Java design Java is to compare two objects is not the same object.

    For reference variables, the two reference variables refer to the same object when compared, that is, the address of the object stored in two references is not the same.

    For the basic data type, the comparison is two data is not equal, no ambiguity.

    Because there is no method for the base data type, there is no problem with equal () and hashcode (), and the following discussion is for reference types.


  2. About Equals ()

    Why does Java design the Equals () method?

    = = Compared to whether two objects are the same object, this does not meet many requirements. Sometimes when two objects are not = =, we still think that the two are "equal", for example, for a string object, when the string sequence of two objects is constant, we think they are "equal". For such a requirement, equals () is required to implement. For classes with objects of this kind, overriding their equals () method allows specific "equality" logic to be defined on its own.

    Places to be aware of

    The default implementation of Equals () in object is to compare two objects that are not = = and that their and = = effects are the same.

    Some of the classes provided by Java have overridden the Equals () method. Write your own class, and if you need to implement your own "equal" logic, you need to rewrite the Equals () method.

  3. About Hashcode ()

    Why is the Hashcode () method designed?

    The Hashcode () method returns a value that we call hashcode. As you can see from the name of the method, the purpose is to generate a hash code. The main purpose of hash code is to hash the object as a key input, it is easy to infer that we need each object hash code as much as possible, in order to ensure the hash of the access performance. In fact, the default implementation provided by the object class does guarantee that each object's hash code is different (a hash code is returned by a particular algorithm based on the object's memory address).

    Analysis to this place, seemingly no problem, the role of the three is clear, as if they have nothing to do with. In the Java specification, the Hashcode () method and the Equals () method do not have a relationship.

    But!!!!!!!! There is a problem.

    The problem is as follows: For Class HashSet, HashMap, and hash related classes (take HashSet as an example), hash the object by hash algorithm. For HashSet, the process of depositing objects is: According to the object's hash code, through the hash algorithm, find where the object should be stored, if the location is empty, then the object is stored in that position, if the location is not empty, then use Equals () to compare the location of the object and the object to be entered, If two are equal, they are no longer inserted, and if not equal, the object is inserted into another location based on the hash conflict resolution algorithm.

    Java rules for hashset to judge whether the duplicate object is done by the Equals () method, which requires that the Equals () method of the two objects equal, the hash code must be equal (that is, hashcode () returns the same value). Assuming that the Equals () method of the two objects is equal, the hash code is unequal, and two objects equal to equals () are inserted into the hashset, which is not allowed. So we have a little bit of a conclusion:

    Conclusion: for two objects equal to Equals (), their hashcode () must return a value equal

The difference between = =, equals, and Hashcode in Java

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.