What is the difference between "= =", Equals and Hashcode __java

Source: Internet
Author: User

1 the "= =" operator is used to compare the values of two variables for equality. That is, the operator is used to compare the values stored in the corresponding memory of a variable to compare the two basic types of data or whether two reference variables are equal, only the "= =" operator is used.

Specifically, if two variables are basic data types, you can use the "= =" operator to compare their corresponding values for equality. If a variable points to data that is an object (a reference type), then two blocks of memory are involved, the object itself occupies a piece of memory (heap memory), and the variable occupies a block of memory, for example, for an assignment statement:

string s = new string ();
The variable s takes up a piece of storage space, and the new String is stored in another storage space, at which point the value stored in the memory of the variable s corresponds to the first address of that block of memory that the object occupies. For variables that point to the object type, if you want to compare whether two variables are pointing to the same object, that is, whether the values in memory for the two variables are equal (whether they point to the same storage space), you can compare them with the "= =" operator. However, if you want to compare the contents of these two objects to be equal, then the "= =" operator cannot be implemented.

2 equals is one of the methods provided by the object class. Each Java class is integrated from the object class, so each object has the method of equals. In the case of the Equals (object) method defined in the object class, Equals (object) is the same as the "= =" operator, compared to a reference.

Compared to the "= =" operator, the Equals (object) method is unique in that it can be overridden, so it is possible to compare data content by overriding the method instead of comparing it, such as whether the Equals method of the string class is used to compare the contents of two separate objects. That is, the contents of the heap are the same, taking the following code as an example:

string S1 = new String ("Hello");
		String s2 = new String ("Hello");
The two new statement creates two objects. Then with S1, S2 These two variables point to an object, this is two different objects, their first address is different, that is, S1 and S2 stored in the number is not the same, so the expression S1==s2 will return False, The contents of these two objects are the same, so the expression s1.equals (S2) returns true.


If a class does not define the Equals () method itself, it inherits the Equals () method of the object class, and the implementation code for the Equals () method of the object class is as follows:

Boolean equals (object) {return
		this==object;
	}
As you can see from the above example, if a class does not define the Equals () method itself, its default equals () Method (inherited from the object class) is to use the "= =" operator, and to compare whether two variables point to the same object, use Equals () Method and using the "= =" operator will get the same result. If two separate objects are compared, the total returns false. If you write a class that wants to be able to compare the contents of the two instance objects created by the class, you must overwrite the Equals () method, and the developer will write their own code to determine under what circumstances the two objects ' contents are considered to be the same.

3 The Hashcode () method is inherited from the object class and is also used to identify whether two objects are equal. The Hashcode () method in the object class returns an int value of an object in memory, so the Hashcode () method of any object is not equal if the Hashcode () method is not overridden.


Although the Equals () method is also used to determine whether two objects are equal, it differs from the Hashcode () method. Generally speaking, the Equals () method is called to the user, and if you need to determine whether two objects are equal, you can override the Equals () method and then call in your code so that you can determine whether they are equal. For the Hashcode () method, the user generally does not call it, for example, in HashMap, because the key is not repeatable, it determines whether the key is repeated to determine the Hashcode () method, but also use the Equals () method. "Cannot repeat" here refers to equals () and hashcode () as long as there is an unequal. So, the Hashcode () method is equivalent to the encoding of an object, like the MD5 in a file, which differs from the Equals () method in that it returns an int and is not intuitive to compare.


Overriding the Equals () method is generally overridden by overwriting the Hashcode () method, otherwise it violates the General convention of Object.hashcode, causing the class to fail with all hash-valued (hash) collection classes (HashMap, HashSet and Hashtable) are combined to function properly.


The relationship between the return value of the Hashcode () method and the Equals () method is as follows:

X.equals (Y) returns true that two objects are equal according to the Equals () method, so the Hashcode () method that calls either of these two objects must produce the same integer result. If X.equals (Y) returns false, that is, two objects are not equal according to the Equals () method, the return value of the Hashcode () method of x and Y may or may not be equal. Conversely, the return value of the Hashcode () method is not equal, the return value of the Equals () method is not equal, and the return value of the Hashcode () method is equal, and the return value of the Equals () method may or may not be equal.

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.