[Turn] override equals () method, needs to be rewritten hashcode () method and toString () method

Why should override the hashcode () method and the toString () method

A, toString ():

Hash code code defines toString () method of the Object class returned when an object inside, the properties of this hashcode code can not be expressed in simple objects. So to override the toString () method.
When it is desired to display an object output, usually to call his toString () method to convert a string .java content of the object class all have a default toString () method.
By default System.out.println (object name) or System.out.println (object name .toString ()) output is the class name of this object and the object corresponding to the first address of memory if you want to customize the output information must be rewritten toString () method.

Precautions:

1. must be declared as public

2. The return of type String

3. The name of the method must be toString, and no parameters

The method not using the output method body System.out.println ()

 

Two, hashCode ():

equals in class Object which defines the comparison is whether the object reference point to the same memory address. Compare his values ​​are the same when comparing two objects in general, so to be rewritten. After overwriting is to compare two objects are equal to the value argument. In particular, when comparing the use of eight packed objects equals (e.g., int, float, etc.) and the String Class (as that class is overridden methods equals and hashcode) object, the default value is compared, in comparison to other objects are referenced to compare address.

Note:
1. reflexive: for any reference value x, of x.equals (x) is a constant to true
2. Symmetry: for any reference values x  and  y, when x.equals (y) returns to true, Y .equals (x) must also return to true
3. transitive: for any reference to the value of X, Y and z, if x.equals (y) returns true, and y.equals (z) returns true, then x.equals (z) must also be returned  back  to true 4. consistency: reference value for any x  and  y, if a subject information has not been modified equals comparisons, multiple calls x.equals (y) returns consistently either true, or consistently return false 5. the non-empty: for any non-null reference value X, of x.equals (null) must return false  

Reproduced: https://www.cnblogs.com/yan217/p/5664895.html .

A, toString ():

Hash code code defines toString () method of the Object class returned when an object inside, the properties of this hashcode code can not be expressed in simple objects. So to override the toString () method.
When it is desired to display an object output, usually to call his toString () method to convert a string .java content of the object class all have a default toString () method.
By default System.out.println (object name) or System.out.println (object name .toString ()) output is the class name of this object and the object corresponding to the first address of memory if you want to customize the output information must be rewritten toString () method.

Precautions:

1. must be declared as public

2. The return of type String

3. The name of the method must be toString, and no parameters

The method not using the output method body System.out.println ()

 

Two, hashCode ():

equals in class Object which defines the comparison is whether the object reference point to the same memory address. Compare his values ​​are the same when comparing two objects in general, so to be rewritten. After overwriting is to compare two objects are equal to the value argument. In particular, when comparing the use of eight packed objects equals (e.g., int, float, etc.) and the String Class (as that class is overridden methods equals and hashcode) object, the default value is compared, in comparison to other objects are referenced to compare address.

Note:
1. reflexive: for any reference value x, of x.equals (x) is a constant to true
2. Symmetry: for any reference values x  and  y, when x.equals (y) returns to true, Y .equals (x) must also return to true
3. transitive: for any reference to the value of X, Y and z, if x.equals (y) returns true, and y.equals (z) returns true, then x.equals (z) must also be returned  back  to true 4. consistency: reference value for any x  and  y, if a subject information has not been modified equals comparisons, multiple calls x.equals (y) returns consistently either true, or consistently return false 5. the non-empty: for any non-null reference value X, of x.equals (null) must return false  

Reproduced: https://www.cnblogs.com/yan217/p/5664895.html .

Guess you like

Origin www.cnblogs.com/day1day1up/p/11128850.html