Java Note = =, equals, hashcode () the Difference

Source: Internet
Author: User

The difference between = =, Equals, Hashcode ():

Basic data types: compare with = =, compare their values
Composite data types: when compared with = =, the comparison is that they are stored in memory address, unless it is the same new object, they are the result of the comparison is true, otherwise false.

The Equals () method is defined in the object base class, and its initial behavior is to compare their memory addresses (just like the = = function), but they are overridden in some class libraries, such as the Equals () method of the string class to compare their contents. The Equals () method is defined in the object class as follows:
public boolean equals (Object obj) {
return (this = = obj);
}

As you can see, Equals is the same address that compares two objects. But string, double, and so on have overridden the Equals method, which compares the object's content to the same.

public native int hashcode ();
The description is a local method, and its implementation is dependent on the local machine. Many classes override this method, for example, the string class overrides the method and calculates a hash value based on its contents.

The hashcode of two objects is the same, and does not necessarily mean that two objects are the same, which is not necessarily applicable to the Equals (Java.lang.Object) method, only that the two objects are in the hash storage structure, such as Hashtable, they " stored in the same basket. "

So what is the relationship between equals and hashcode:

1. If two objects are the same, which is applicable to the Equals (Java.lang.Object) method, then the hashcode of the two objects must be the same;

2. Equals () is not equal to two objects, but does not prove that their hashcode () are unequal. In other words, the Equals () method is not equal to two objects, and hashcode () may be equal. (My understanding is that the hash code is generated when the conflict is created);

3.hashcode (), must be able to launch equals () also unequal; Hashcode () is equal, Equals () may or may not be equal.

Java Note = =, equals, hashcode () the Difference

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.