Comparing Objects : equals « Class Definition « Java Tutorial






public class MainClass {
  public static void main(String[] args) {
    Employee emp1 = new Employee("M", "A");
    Employee emp2 = new Employee("M", "A");
    if (emp1.equals(emp2))
      System.out.println("These employees are the same.");
    else
      System.out.println("These are different employees.");
  }
}

class Employee {
  private String lastName;

  private String firstName;

  public Employee(String lastName, String firstName) {
    this.lastName = lastName;
    this.firstName = firstName;
  }

  public String getLastName() {
    return this.lastName;
  }

  public String getFirstName() {
    return this.firstName;
  }

  public boolean equals(Object obj)

  {
    if (this == obj)
      return true;
    if (this == null)
      return false;

    if (this.getClass() != obj.getClass())
      return false;

    Employee emp = (Employee) obj;
    return this.lastName.equals(emp.getLastName()) && this.firstName.equals(emp.getFirstName());
  }
}








5.19.equals
5.19.1.Comparing Objects
5.19.2.Implement equals method using commons-lang
5.19.3.Use CompareToBuilder class to create compareTo method for your own class
5.19.4.Define your own equals method