You can not overload the ==
operator, but you can override equals(Object)
if you want it to behave differently from the ==
operator, i.e. not compare references but actually compare the objects (e.g. using all or some of their fields).
Also, if you do override equals(Object)
, have a look at hashCode()
as well. These two methods need to be compatible (i.e. two objects which are equal according to equals(Object)
need to have the same hashCode()
), otherwise all kinds of strange errors will occur (e.g. when adding the objects to a set or map).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…