Difference Between Equality of Objects and Equality of References in Java


In Java, when managing objects, it's important to get the qualification between equality of objects and equality of references. Whereas both concepts include comparisons, they work on distinctive levels. Uniformity of objects decides whether two objects have the same substance, whereas balance of references decides whether two factors refer to the same protest in memory. This article will dig into the language structure, approaches, and code cases to demonstrate the distinction between these two shapes of correspondence in Java.

Syntax

To understand the difference between equality of objects and equality of references, let's start with the syntax used in Java −

Equality of Objects

object1.equals(object2)

Explanation

This syntax invokes the equals() method, which is a standard Java method available in all objects. It compares the content of two objects to determine if they are equal. The equals() method is often overridden by classes to define their specific notion of equality.

Equality of References

Syntax

object1 == object2

Explanation

The == operator compares the references of two objects to check if they point to the same memory location. It assesses to be genuine in case both references point to the same object, and false otherwise.

Explanation of Syntax

  • Equality of Objects

    When utilizing object1.equals(object2), the equals() strategy is called on object1 and passed object2 as a contention. This strategy compares the substance of object1 with the substance of object2 to  determine if they are considered equal. The equals() strategy can be overridden in custom classes to supply a customized definition of equality based on particular object properties.

  • Equality of References

    The object1 == object2 syntax compares the references of object1 and object2 directly. It checks whether the memory addresses held by the references are the same. In the event that the references are indicating to the same object, the result is genuine. If they are pointing to different objects, the result is false.

Approach 1: Equality of Objects

Algorithm

  • Retrieve the objects that need to be compared.

  • Use the equals() method to compare the content of the objects.

  • If the equals() method returns true, the objects are considered equal. Otherwise, they are not.

Example

public class EqualityOfObjectsExample {
   public static void main(String[] args) {
      String name1 = "John";
      String name2 = "John";
        
      boolean isEqual = name1.equals(name2);
        
      System.out.println("Are the objects equal? " + isEqual);
   }
}

Output

Are the objects equal? true

Explanation of the Code in Approach 1

Within the given code case, we have two String objects, name1 and name2. We utilize the equals() strategy on name1 and pass name2 as an argument. The result is stored in the isEqual variable. Finally, we print the outcome, which will indicate whether the objects are equal or not. In this case, since both objects have the same content ("John"), the equals() method will return true, and the output will be "Are the objects equal? true."

Approach 2: Equality of References

Algorithm

  • Retrieve the objects that need to be compared.

  • Use the == operator to compare the references of the objects.

  • If the references are the same, the objects are considered equal. Otherwise, they are not.

Example

public class EqualityOfReferencesExample {
   public static void main(String[] args) {
      String name1 = new String("John");
      String name2 = new String("John");
        
      boolean isSameReference = (name1 == name2);
        
      System.out.println("Are the references the same? " + isSameReference);
   }
}

Output

Are the references the same? false

Explanation of the Code in Approach 2

In this code illustration, we have two String objects, name1 and name2, which are made utilizing the modern keyword. Even though the objects have the same content ("John"), they are distinct instances in memory. We compare the references of these objects using the == operator and store the result in the isSameReference variable. The output will indicate whether the references are the same or not. In this case, since the objects are distinctive occasions, the == operator will return false, and the yield will be "Are the references the same? false."

Difference Between Equality of Objects and Equality of References in Java

Title

Equality of Objects

Equality of References

Definition

Compares the content of two objects.

Compares the memory references of objects.

Syntax

object1.equals(object2)

object1 == object2

Comparison Basis

Object content

Object references

Customization Ability

Yes (via overriding equals() method)

No

Outcome

Indicates if objects have the same content.

Indicates if object references are the same.

Conclusion

In Java, it's fundamental to recognize the correspondence between objects and uniformity of references. The breaks even with() strategy decides whether the substance of two objects is the same, whereas the == operator compares the memory references of two objects. By understanding these concepts, you'll type in code that precisely handles question comparisons based on your particular necessities. Remember to use the appropriate approach depending on whether you want to compare object content or object references.

Updated on: 28-Jul-2023

105 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements