I constructed a class with one String
field. Then I created two objects and I have to compare them using ==
operator and .equals()
too
==
compares object references, it checks to see if the two operands point to the same object (not equivalent objects, the same object).
If you want to compare strings (to see if they contain the same characters), you need to compare the strings using equals
.
In your case, if two instances of MyClass
really are considered equal if the strings match, then:
public boolean equals(Object object2) {
return object2 instanceof MyClass && a.equals(((MyClass)object2).a);
}
...but usually if you are defining a class, there's more to equivalency than the equivalency of a single field (a
in this case).
Side note: If you override equals
, you almost always need to override hashCode. As it says in the equals JavaDoc:
Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.