I constructed a class with one String field. Then I created two objects and I have to compare them using == operator and .equals() too
If you dont need to customize the default toString() function, another way is to override toString() method, which returns all attributes to be compared. then compare toString() output of two objects. I generated toString() method using IntelliJ IDEA IDE, which includes class name in the string.
public class Greeting {
private String greeting;
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
return this.toString().equals(obj.toString());
}
@Override
public String toString() {
return "Greeting{" +
"greeting='" + greeting + '\'' +
'}';
}
}