1) the "= =" operator compares the values of two variables for equality. That is, the operator is used to compare the values stored in the memory of the variable for equality, to compare two underlying types of data or two reference variables for equality, and only use the "= =" operator.
Specifically, if two variables are the underlying type, you can use the "= =" operator directly to determine whether the corresponding values are equal. If a variable points to an object (reference type), then two memory is involved, the object itself occupies a block of memory (heap memory), and the variable occupies a piece of memory, for example, the statement String s = new string (), the variable s occupies a block of storage space, and the new string () Occupies another piece of storage space, where the corresponding memory value of the variable s corresponds to the first address of the block of memory that the object occupies. For a variable that points to a type, if you determine whether two variables point to the same object, that is, to compare whether the values in memory for the two variables are the same (whether the two variables point to the same variable), you can compare them by the "= =" operator, but if you want to compare the contents of the two objects for equality, use the "= =" can not be achieved.
2) equals is a method in object objects, each Java class inherits from object, so each object has the Equals method, and the Equals method in Object uses the "= =" operator directly to compare two objects. So in the case where the Equals method is not overridden, equals is the same as the "= =" result, which is a reference to the comparison object.
In contrast to the "= =" operator, the Equals method is unique in that it can be overwritten, so it can be overridden so that the method does not compare references, but rather the data content we need, for example, the Equals method in the string class is used to compare the contents of two separate objects. That is, if the contents of the heap are equal, take the following code as an example:
String s1= new String ("Hello");
String s2= new String ("Hello");
Two new statements create two objects, then point to an object with S1 and S2, which are two different objects whose first address is not the same, that is, the values stored by S1 and S2 are not the same, so the expression S1==s2 will return false, and the contents of the two objects are the same. So the Equals method returns True.
The above example shows that if a class does not have a replication Euqals method, it inherits the Equals method from object, and by default uses the "= =" operator to compare, that is, if the reference to two objects is the same, then equals and = = will get the same result. If you compare two independent objects, you will always return FALSE. If the class you are writing wants to be able to compare the contents of the two instance objects created by the class for equality, you must override the Equals method.
3) The Hashcode () method is inherited from the object class and it also identifies whether two objects are equal, and hashcode in the object class returns an int value of the address of the objects in memory, so when the Hashcode method is not overridden, The hashcode of any object are not the same.
Although equals is also a comparison of two objects, it is different from the Hashcode method. In general, the Equals method is used by the user, if you want to determine whether two objects are equal, you need to overwrite the Equals method and then call it in the code to determine if they are equal. For hashcode methods The user typically does not invoke it, for example in HashMap, because key is not duplicated, it calls the Hashcode method when it determines whether the key is duplicated, and it also uses the class Equals method. This cannot be repeated here, meaning that one of the Equals and Hashcode methods is not the same. So hashcode is equal to an object's encoding table, just like the MD5 in a file, it differs from the Equals method in that its return value is of type int and is not intuitive to compare.
It is common to overwrite the Hashcode method while overriding the Equals method, otherwise it violates the General Rules Convention of Object.hashcode, resulting in the inability to match all hash-list-based collection classes (HashMap HashSet hashTable) Used in conjunction with normal.
The relationship between the Hashcode method and the Equals method is as follows: If the return value of X.equals is true, that is, two objects are the same by the Equals method, then the Hashcode method that calls both returns the same int value. if the return value of X.equals is false, that is, the result of two objects being compared by the Quals method is not the same, then the return value of x, y hashcode may or may not be equal. Conversely, the return value of the hashcode is not equal, it is certain that the return value of the Equals method is unequal, and the return value of the hashcode is equal, and the return value of equals may or may not be equal.
What is the difference between java-"= =", Equals and hashcode?