equals depth understanding

package cn.galc.test;

{class TestEquals public
public static void main (String [] args) {
/ **
* constructor used herein Cat () new cats in a heap memory inside,
* which two cats color, weight, height are the same,
* but c1 and c2 but never equal, because c1 and c2 are two cats inside the heap memory of the referenced object,
* which contained two cats can find this address, but because of two cats in inside heap memory is stored in two different spaces inside,
* so c1 and c2 are filled with different addresses, c1 and c2 will never be the same.
* /
Cat Cat new new C1 = (. 1,. 1,. 1);
Cat Cat new new C2 = (. 1,. 1,. 1);
System.out.println ( "c1 == c2 result is:" + (c1 == c2 )); to false //
System.out.println ( "results c1.equals (c2) is:" + c1.equals (c2)); // to false
}
}

class Cat {
int color, weight, height;

Cat public (Color int, int weight, int height) {
this.color = Color;
this.weight = weight;
this.height = height;
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
shown in FIG memory analysis c1 and c2 analysis result of the comparison, when performing Cat c1 = new Cat (1,1,1) ; Cat c2 = new Cat (1,1,1) ; during and after the memory layout as shown below:
         

? From this we see that when we have a new object, load a copy of its own memory in memory rather than sharing! For static variables and methods modified method is stored in the area, only loaded once, not one more copy memory.
So whether we are equal on both a target logical judgment, that the contents of the object can not be used directly for equality in inheritance equals Object class () method, we have to override the equals () method, this method to change the default implementation. The following override inherited equals the inside of Cat () method:
class Cat {
int Color, weight, height;

public Cat(int color, int weight, int height) {
this.color = color;
this.weight = weight;
this.height = height;
}

/ **
* This is inherited from the Object class equal rewritable down equals () method, a method to change the default implementations,
* defined by implementing our own decision to determine whether two objects are logically equivalent.
* Here we define if two cats of color, weight, height are the same,
* then we think that cats are logically identical, namely that two cats are "equal" of.
* /
Public Boolean the equals (Object obj) {
IF (obj == null) {
return to false;
}
the else {
/ **
* is the object of the instanceof operator.
* Operator is used to determine whether the object object belongs to a specified instance of a class or subclass specific.
* If the object on the left is the right of the object class was created, the computed result is true, otherwise false.
* /
IF (the instanceof Cat obj) {
Cat C = (Cat) obj;
IF (c.color == == this.weight c.weight this.color && && c.height == this.height) {
return to true;
}
}
}
return to false;
}
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
? Design idea is very simple: the comparison object is first determined whether null-> Judgments whether the object is an instance of the class to compare -> comparison of two members of the variables are exactly equal.
Another common method of rewriting //
@Override
public Boolean the equals (Object obj) {
IF (the this == obj) return to true;
IF (obj == null) return to false;
! IF (getClass () = obj.getClass ( http://www.amjmh.com/v/BIBRGZ_558768/)) return false;
= OTHER People (People) obj;
IF (Age = other.age!) Return to false;
IF (firstName == null) {
IF (! Other.firstName = null) return to false;
}! The else IF (firstName.equals (OTHER .firstName)) return to false;
IF (lastName == null) {
IF (other.lastName = null) return to false;!
} the else IF (lastName.equals (other.lastName)) return to false;!
return to true;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
? so by overriding equals () method in the class, we can compare different under the same class of the objects are equal.
---------------------

Guess you like

Origin www.cnblogs.com/ly570/p/11331584.html