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