Let’s say I have two objects, A and B where..
Object A=new Object();
Object B=A;
These objects by default each have two ints: int X and int Y. First, in both A and B,
(X == 0) && (Y == 0)
So, you would say those two are equal, as would Java. Now, let’s say we change A.X so that A.X=2. Now, A and B are no longer equal since
A.X==2
..but..
B.X==0
Java, however, still says they are equal.
(A.equals(B)) == true
(B.equals(A)) == true
So, how do you get around that?
By doing this
Object B=A;, you are not creating a new object, butBis pointing toAonly. So its only one object.So when you change
A.X = 2,B.Xis also 2 at its referring the same variable and hence equal.You may verify this by printing
B.Xvalue.