• Home
  • Docker
  • Kubernetes
  • LLMs
  • Java
  • Ubuntu
  • Maven
  • Big Data
  • Archived
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:

    The java.util.Objects class provides utility methods to check if two objects are equals or deeply equals (in case of arrays):


    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":

    • Subclass "C":
© 2025  mtitek