Methods that are common to all objects
Follow common conventions When overriding equals
Each instance of a class is inherently unique.
Does not care if the class provides a logical equivalent test function
The superclass has overridden equals, and the behavior inherited from the superclass is also appropriate for subclasses.
Class is private or package-level private, it can be determined that its Equals method is never called.
throw new Assertionerror ()
General coverage Object.Equals are value classes
However, there is a value class that does not need to overwrite the Equals method, that is, the instance is controlled, ensuring that each value has a class of at most one object. such as enumeration
Overrides the Equals method, a common convention.
Reflexive nature
Symmetry of
Transitivity
Consistency
Non-void sex
The principle of the Richter replacement
Any important property of a type will also apply to its subtypes.
Tips for achieving a high-quality equals method
1. Use the = = operator to check if "parameter is a reference to this object"
2. Use the instanceof operator to check if the parameter is the correct type
3. Convert the parameters to the correct type
4. For each critical field in the class, check that the field in the parameter matches the corresponding field in the object.
Overwrite equals when always overwrite Hashcode
Do not attempt to make the Equals method too intelligent
Do not replace object objects in the equals declaration with other types.
Overwrite equals when always overwrite Hashcode
Conventions:
During application execution, the Hashcode method must consistently return the same integer if the information used for the comparison of the object's Equals method has not been modified, and the same object is called multiple times. During multiple executions of the same application, the integer returned by each execution can be inconsistent.
If the object is equal according to the Equals (object) method, the Hashacode method that calls either object in both objects must produce the same integer result.
If two objects are not equal according to the Equals (object) method, then calling the Hashcode method of any of these two objects does not necessarily produce a different integer result. However, programmers should know that it is possible to improve the performance of a hash table by producing distinct integer results for unequal objects.
Hash.put
Hash.get
Can't get
There is no value in writing a hashcode method that is legitimate but not useful.
In the calculation of the hash code, the redundant domain can be excluded.
Effective Java Chapter III common methods for all objects (i) reading notes