Java Object equals Method Example

In this example, we will demonstrate the usage of java.lang.Object.equals() method with an example.

Object equal() Method Examples

The java.lang.Object.equals(Object obj) indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:
  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

1. Simple Object equals() Method Example

Let's create a simple example to demonstrate the usage of the equals() method:
// get an integer, which is an object
  Integer x = new Integer(50);

  // get a float, which is an object as well
  Float y = new Float(50f);

  // check if these are equal,which is 
  // false since they are different class
  System.out.println("" + x.equals(y));

  // check if x is equal with another int 50
  System.out.println("" + x.equals(50));
Output:
false
true

2. Object equals() Method Example

public class Person {
     private String firstName;
     private String lastName;

     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 (this == obj)
          return true;
       if (obj == null)
          return false;
       if (getClass() != obj.getClass())
          return false;
       Person other = (Person) obj;
       if (firstName == null) {
          if (other.firstName != null)
             return false;
       } else if (!firstName.equals(other.firstName))
             return false;
       if (lastName == null) {
          if (other.lastName != null)
             return false;
       } else if (!lastName.equals(other.lastName))
             return false;
       return true;
    }

   @Override
   public String toString() {
       return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
   }
  public static void main(String[] args) throws CloneNotSupportedException {
        Person person = new Person();
        person.setFirstName("Ramesh");
        person.setLastName("Fadatare");

        Person person1 = new Person();
        person1.setFirstName("Ramesh");
        person1.setLastName("Fadatare");

        boolean hasEqual = person.equals(person1);
        System.out.println("Both objects equal :: " + hasEqual);
    }
}
Output:
Both objects equal :: true
The equals() method for Person class is:
@Override
public boolean equals(Object obj) {
    if (this == obj)
       return true;
    if (obj == null)
       return false;
    if (getClass() != obj.getClass())
       return false;
    Person other = (Person) obj;
    if (firstName == null) {
       if (other.firstName != null)
          return false;
    } else if (!firstName.equals(other.firstName))
          return false;
    if (lastName == null) {
       if (other.lastName != null)
          return false;
    } else if (!lastName.equals(other.lastName))
          return false;
    return true;
}
Note: If you override equals(), you must override hashCode() as well.

Reference


Comments