Given a class below :
1 | // Can you spot the bug? (Page 188) |
If we don’t add Override annotation, equals method will not work.
Actually we want to override equals and even hashCode method, however, there is no @Override annotation.
Also the type of parameter we pass into equals method is wrong,(should be Object).
So overloading happens instead of overriding.
Whatever we do in Set s, s deals with different element within it by Object.equals method :
1 | /** |
So there are different 260 objects in s.
Properties of equals function
- reflexive
- symmetric
- transitive
- consistent
- non-null x => x.equals(null) => false
- x,y => refer to the same object => true
- remember to add @Override and override hashCode method
clutter : 混乱
deem : 认为