The Wayback Machine - https://web.archive.org/web/20240615050053/https://www.geeksforgeeks.org/difference-between-equality-of-objects-and-equality-of-references-in-java/
Open In App

Difference Between Equality of Objects and Equality of References in Java

Last Updated : 17 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Equality of objects means when two separate objects happen to have the same values/state. Whereas equality of references means when two object references point to the same object. The == operator can be used to check if two object references point to the same object. To be able to compare two java objects of the same class the boolean equals(Object obj) method must be overridden and implemented by the class. The implementer decides which values must be equal to consider two objects to be equal.

In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.

Java




import java.io.*;
  
String obj1 = new String("xyz");
String obj2 = obj1;
  
// now obj2 and obj1 reference the same place in memory
  
if (obj1 == obj2)
    System.out.println("obj1==obj2 is True");
else
    System.out.println("obj1==obj2 is False");


Output:

obj1==obj2 is True

Note in the code above that obj2 and obj1 both reference the same place in memory because of this line: “String  obj2=obj1;”. And because the “==” compares the memory reference for each object, it will return true.

The equals method is defined in the Object class, from which every class is either a direct or indirect descendant. By default, the equals() method actually behaves the same as the “==” operator – meaning it checks to see if both objects reference the same place in memory. But, the equals method is actually meant to compare the contents of 2 objects and not their location in memory.

The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory. This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.

Java




import java.io.*;
  
String obj1 = new String("xyz");
String obj2 = new String("xyz");
  
if (obj1.equals(obj2))
    System.out.println("obj1==obj2 is True");
else
    System.out.println("obj1==obj2 is False");


Output:

obj1==obj2 is True


Similar Reads

How to Create a LinkedHashSet with Custom Equality for Objects?
In this article, we want to create a LinkedHashSet of custom objects with specific equality criteria. So, we can create it by overriding the equals and hashcode methods in Java. In the equals() method, we can put the custom criteria over there and we override the hashcode to provide the same Hashcode to our custom object. Example:We want to impleme
3 min read
Method References in Java with examples
Functional Interfaces in Java and Lambda Function are prerequisites required in order to grasp grip over method references in Java. As we all know that a method is a collection of statements that perform some specific task and return the result to the caller. A method can perform some specific task without returning anything. Methods allow us to re
9 min read
Can an ArrayList Contain Multiple References to the Same Object in Java?
ArrayList class in Java is basically a resizable array i.e. it can grow and shrink in size dynamically according to the values that we add or remove to/from it. It is present in java.util package. We will discuss if an ArrayList can contain multiple references to the same object in Java. The ArrayList in java does not provide the checks for duplica
2 min read
Types of References in Java
In Java there are four types of references differentiated on the way by which they are garbage collected. Strong References Weak References Soft References Phantom References Prerequisite: Garbage Collection Strong References: This is the default type/class of Reference Object. Any object which has an active strong reference are not eligible for ga
4 min read
C/C++ Pointers vs Java References
Java doesn't have pointers; Java has references. Reference: A reference is a variable that refers to something else and can be used as an alias for that something else. Pointer: A pointer is a variable that stores a memory address, for the purpose of acting as an alias to what is stored at that address. So, a pointer is a reference, but a reference
5 min read
Equality (==) operator in Java with Examples
== operator is a type of Relational Operator in Java used to check for relations of equality. It returns a boolean result after the comparison and is extensively used in looping statements and conditional if-else statements. Syntax: LHS value == RHS value But, while comparing these values, three cases arise generally: Case 1: When both LHS and RHS
4 min read
Difference Between java.sql.Time, java.sql.Timestamp and java.sql.Date in Java
Across the software projects, we are using java.sql.Time, java.sql.Timestamp and java.sql.Date in many instances. Whenever the java application interacts with the database, we should use these instead of java.util.Date. The reason is JDBC i.e. java database connectivity uses these to identify SQL Date and Timestamp. Here let us see the differences
7 min read
Difference between Lodash _.clone() method and '=' operator to copy Objects
In this article, we will learn about the difference between using the _.clone() method in Lodash and using the '=' operator to copy objects. Both of these methods are used to create a copy of an object in JavaScript. However, both work in very different ways. Using _.clone() Method: The _.clone() method creates a new object and copies each value fr
3 min read
Difference between Abstract Data Types and Objects
1. Abstract data type (ADT) :An Abstract data type (ADT) is a mathematical model for data types. We can think of Abstract Data Type as a black box. As black box hides internal structure, ADT hides the design of data type. An abstract data type's behavior (semantics) can be specified from the perspective of a data user, specifically in terms of pote
2 min read
Difference between native, host and user objects
JavaScript objects are broadly classified into the following categories: native javascript objects, user objects, and host javascript objects. Native objects: Native javascript objects are standard javascript objects which are provided by javascript itself. They are also known as built-in objects, pre-defined objects, or global objects. In JavaScri
3 min read
Article Tags :
Practice Tags :