Source: Java Pathshala - Wisdom Being Shared Blog

Java Pathshala - Wisdom Being Shared Blog Equals() Override

Equals ()• equals() method is used to compare Objects for equality • Default implementation of equals() class provided by java.lang.Object compares memory location and only return true if two reference variable are pointing to same memory location i.e. essentially they are same object.• equals method in Java should be:o Reflexive : Object must be equal to itself.o Symmetric : if a.equals(b) is true then b.equals(a) must be true.o Transitive : if a.equals(b) is true and b.equals(c) is true then c.equals(a) must be true.o Consistent : multiple invocation of equals() method must result same value until any of properties are modified. So if two objects are equals in Java they will remain equals until any of their property is modified.o Null: Comparing any object to null must be false & should not result in NullPointerException. For example a.equals (null) must be false, passing unknown object, which could be null, to equals in Java is actuallySteps to override equal ()• Do this check - if yes then return true.• Do null check - if yes then return false.• Do instance-of check, if instance-of returns false than return false from equals in Java. Also instead of instance-of we can use getClass () method for type identification because instance-of check returns true for subclass also, so it's not strictly equals comparison until required by business logic. But instance-of check is fine if your class is immutable and no one is going to sub class it. For example, we can replace instance-of check by below codeif((obj == null) || (obj.getClass() != this.getClass())) return false;• Type cast the object; note the sequence instanceof check must be prior to casting object.• Compare individual attribute starting with numeric attribute because comparing numeric attribute is fast and use short circuit operator for combining checks. If first field does not match, don't try to match rest of attribute & return false. It's also worth to remember doing null check on individual attribute before calling equals () method on them recursively to avoid NullPointerException during equals check in Java.public class Person { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id =id;} public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName =firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName =lastName; } @Override public boolean equals(Object obj) { if (obj== this) { return true; } if (obj== null ||obj.getClass() != this.getClass()) { return false; } Person guest = (Person) obj; return id == guest.id && (firstName== guest.firstName || (firstName != null && firstName.equals(guest.getFirstName()))) && (lastName== guest.lastName || (lastName != null && lastName.equals(guest.getLastName()))); } @Override public int hashCode() { final int prime= 31; int result= 1; result = prime * result + ((firstName== null) ? 0 :firstName.hashCode()); result = prime * result+ id; result = prime * result + ((lastName== null) ? 0 :lastName.hashCode()); return result; }}Tips for equals ()• If your domain class has any unique business key then just comparing that field in equals method would be enough instead of comparing all the fields e.g. in case of our example if "id" is unique for every Person and by just comparing id we can identify whether two Person are equal or not.• While overriding hashCode in Java makes sure you use all fields which have been used in equal's method in Java.• String and Wrapper classes like Integer, Float and Double override equals method but StringBuffer doesn't override it.• Whenever possible try to make your fields immutable by using final variables in Java, equals method based on immutable fields are much secure than on mutable fields.

Read full article »
Est. Annual Revenue
$100K-5.0M
Est. Employees
1-25