Two objects are considered to be equal when they are identical (contain the same data) or in other words they are in the same state.
In order to compare two objects for equality, we need to override
It does not make member level comparison. Following is the piece of code of
Sample of equals() method
0 1 2 3 4 |
public boolean equals(Object obj) { return (this == obj); } |
The above implementation of
If two objects having same data are stored at different locations in memory then above implementation will return false. So, when we define our own data types (classes), we need to override

Java’s convention is that
- Reflexive:
x.equals(x)
is true. - Symmetric:
x.equals(y)
is true if and only if y.equals(x). - Transitive: if
x.equals(y)
andy.equals(z)
are true, then so isx.equals(z)
.
In addition to the above rules,
- Consistent: multiple invocations of
x.equals(y)
consistently return the same value, provided neither object is modified. - Not null:
x.equals(null)
returns false.