• Home
  • Cloud
  • Big Data
  • CI
  • Install
  • Samples
  • Java
  • Ubuntu
  • Maven
  • Archive
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 show how to implement the equals method. The code provide an implementation of the equals method for a superclass "P" and subclass "C". The code also an implementation of a method (called here same) that check if a superclass and possible a subclass are equals.

    • Superclass "P":

    • Subclass "C":
© 2010-2022  mtitek