Difference Between Equality of Objects and Equality of References in Java Last Updated : 17 Jul, 2022 Comments Improve Suggest changes Like Article Like 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 Comment More infoAdvertise with us Next Article Difference Between Equality of Objects and Equality of References in Java akshaysobti15 Follow Improve Article Tags : Java Difference Between Practice Tags : Java Similar Reads Difference Between == Operator and equals() Method in Java In Java, the equals() method and the == operator are used to compare objects. The main difference is that string equals() method compares the content equality of two strings while the == operator compares the reference or memory location of objects in a heap, whether they point to the same location 4 min read Difference Between Object and Instance in Java The object is an instance of a class. A class is like a blueprint or template that defines the properties and behavior of objects. When we create an object we are creating an instance of that class. Object in JavaThe object is an instance of a class. A class is a blueprint or template that describes 3 min read Difference between HashMap and IdentityHashMap in Java HashMap in java is a class that is a part of the java collection. It implements the Map interface of java. It stores the data in the form of key and value pair. The key should be unique but the value may be duplicate. If you try to insert the duplicate key, it will replace the element of the corresp 4 min read equals() on String and StringBuffer objects in Java Consider the following codes in java: Java // This program prints false class GFG { public static void main(String[] args) { StringBuffer sb1 = new StringBuffer("GFG"); StringBuffer sb2 = new StringBuffer("GFG"); System.out.println(sb1.equals(sb2)); } } Output: false Java // This 1 min read ByteBuffer equals() method in Java with Examples The equals() method of java.nio.ByteBuffer class is used to check whether or not the given buffer is equal to another object. Two byte buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, con 3 min read DoubleBuffer equals() method in Java with Examples The equals() method of java.nio.DoubleBuffer Class is used to check whether or not the given buffer is equal to another object. Two double buffers are equal if, and only if, They have the same element type, They have the same number of remaining elements, and The two sequences of remaining elements, 3 min read AbstractSequentialList equals() method in Java with Example The equals() method of java.util.AbstractSequentialList class is used to check if this AbstractSequentialList object is equal to the object passed as the parameter. This method returns a boolean value stating the same. Syntax: public boolean equals(Object o) Parameters: This method takes the object 2 min read AbstractMap.SimpleEntry equals(Object o) Method in Java with Examples AbstractMap.SimpleEntry<K, V> is used to maintain a key and a value entry. The value can be changed using the equals? method. This class facilitates the process of building custom map implementations. equals(Object o) method of AbstractMap.SimpleEntry<K, V> used to compare the specified 2 min read DateFormat equals() Method in Java with Examples The equals() Method of the DateFormat class is used to compare two DateFormat objects. The method returns True if this DateFormat is equal to the passed object else returns False. Syntax: public boolean equals(Object obj) Parameters: The method takes one parameter obj of Object type and refers to th 2 min read Collator equals(Object) method in Java with Example The equals() method of java.text.Collator class is used to check if both the Collator objects are same or not. Syntax: public boolean equals(Object that) Parameter: This method takes two Collator objects between which comparison is going to take place.Return Value: if both Collator objects are equal 2 min read Like