OOPS Interview Questions · Question 11

What is the difference between object identity and value equality?

Interview preparation resource from Gate Smashers.

Interview-ready answer

Object identity asks whether two references denote the very same object. Value equality asks whether two objects are considered equal by their values or logical state, even when they are separate instances. For example, in Python, `is` checks identity and `==` checks equality; in Java, `==` compares object references while `equals()` compares values when the type implements it accordingly.

Python — identity versus equalitypython
a = [1, 2]
b = a          # Same object
c = [1, 2]     # Different object with equal contents

print(a is b)  # True: same identity
print(a == b)  # True: equal values

print(a is c)  # False: different identities
print(a == c)  # True: equal values

b.append(3)
print(a)       # [1, 2, 3], because a and b refer to the same object
print(c)       # [1, 2]
Java — identity versus equalityjava
import java.util.Arrays;
import java.util.List;

String s1 = new String("x");
String s2 = new String("x");

System.out.println(s1 == s2);       // false: different object references
System.out.println(s1.equals(s2));  // true: same character content

List<String> list = Arrays.asList("a");
System.out.println(list.contains(new String("a"))); // true: uses equals()
Understand it clearly

Object identity

An object has an identity that distinguishes it from every other object instance. Two references have the same identity only when they refer to that exact instance.

When two references denote the same mutable object, a change made through one reference is observable through the other because both access the same object.

Value equality

Value equality means that two objects represent the same value or equivalent state. They may be different instances stored independently.

The precise meaning of equality is defined by the language and the type. For user-defined types, an equality operation should normally be reflexive, symmetric, transitive, and consistent.

Language-specific expressions

The general distinction is universal, but the syntax used to test it depends on the language.

  • Java: `==` compares object references, while `equals()` is used for logical equality.
  • Python: `is` checks identity, while `==` invokes value-equality behavior such as `__eq__`.
  • C#: `ReferenceEquals(a, b)` checks whether two references denote the same object; `Equals` and some overloaded `==` operators can express value equality.
  • C++: Pointer comparison can test whether two pointers refer to the same object, while `operator==` can define value equality for an object type.

Practical use and collection behavior

Use identity when the exact instance matters, such as checking a sentinel or determining whether two references share mutable state. Use value equality when the question is whether two values are logically equivalent.

For hash-based collections, equality and hashing must agree. In Java, types that override `equals()` should provide a compatible `hashCode()`. In Python, hashable objects used as dictionary keys or set elements must follow the language's equality and hashing rules.

Quick comparison
BasisObject identityValue equality
Question answeredAre these two references the same object instance?Do these two objects represent the same value or equivalent state?
Different instancesDifferent instances never have the same identity.Different instances can be equal.
Typical checksPython: `is`; Java: `==` for object references.Python: `==`; Java: `equals()`.
Mutable objectsReferences to the same object observe the same mutations.Two separate objects that are initially equal can cease to be equal if one is mutated.
Typical useShared-instance, sentinel, or reference-specific checks.Comparing domain values and working with value-based collection operations.