• Home
  • LLMs
  • Python
  • Docker
  • Kubernetes
  • Java
  • Maven
  • All
  • About
Java | equals Method
  1. equals Method

  1. equals Method
    When implementing the equals method we should make sure that it's:
    • reflexive (x <-> x)

    • symmetric (x <-> y)

    • transitive (x <-> y && y <-> z => x <-> z)

    • consistent (either always true or always false)

    The call to equals method with null value as argument should return false.

    The equals method is defined in the java.lang.Object class:
    public boolean equals(Object obj) {
        return (this == obj);
    }
    The java.util.Objects class provides utility methods to check if two objects are equals or deeply equals (in case of arrays):
    public static boolean equals(Object a, Object b) {
        return (a == b) || (a != null && a.equals(b));
    }
    public static boolean deepEquals(Object a, Object b) {
        if (a == b)
            return true;
        else if (a == null || b == null)
            return false;
        else
            return Arrays.deepEquals0(a, b);
    }
    The java.util.Arrays class provides utility methods to check if two arrays are equals (including specific overloaded methods for each primitive type). It also provides a utility method to check if two arrays are deeply equals (array of arrays).

    The following is a sample code that shows how to implement the equals method.
    The code provides an implementation of the equals method for a superclass "P" and subclass "C".
    The code also has a custom method (same) that provides a check if two instances of the same class are equal.

    • Superclass "P":
      class P {
          Integer id = 1;
      
          public Integer getId() {
              return id;
          }
      
          @Override
          public boolean equals(Object obj) {
              if (this == obj)
                  return true;
      
              if (obj == null)
                  return false;
      
              if (getClass() != obj.getClass())
                  return false;
      
              P p = (P) obj;
      
              return new EqualsBuilder().append(this.getId(), p.getId()).build();
          }
      
          public boolean same(Object obj) {
              if (this == obj)
                  return true;
      
              if (obj == null)
                  return false;
      
              if (!(obj instanceof P))
                  return false;
      
              P p = (P) obj;
      
              return new EqualsBuilder().append(this.getId(), p.getId()).build();
          }
      }
    • Subclass "C":
      class C extends P {
          String code = "default";
      
          public String getCode() {
              return code;
          }
      
          @Override
          public boolean equals(Object obj) {
              if (!super.equals(obj))
                  return false;
      
              C c = (C) obj;
      
              return new EqualsBuilder().append(this.getCode(), c.getCode()).build();
          }
      
          @Override
          public boolean same(Object obj) {
              if (!super.same(obj))
                  return false;
      
              if (!(obj instanceof C))
                  return false;
      
              C c = (C) obj;
      
              return new EqualsBuilder().append(this.getCode(), c.getCode()).build();
          }
      }
© 2025  mtitek