You are on page 1of 8

identity: a variable holds the same instance as another variable.

equality: two distinct objects can be used interchangeably. they often have the same
id.

For example:

Integer a = new Integer(1);


Integer b = a;

a is identical to b.

Integer c = new Integer(1);


Integer d = new Integer(1);

c is equal but not identical to d.

Of course, two identical variables are always equal.

In Java, equality is defined by the equals method. Keep in mind, if you


implement equals you must also implement hashCode.
Identity determines whether two objects share the same memory address. Equality
determines if two object contain the same state.

If two object are identical then they are also equal but just because two objects are equal
dies not mean that they share the same memory address.

There is a special case for Strings but that is off topic and you'll need to ask someone else
about how that works exactly ;-)
Identity means it is the same object instance while equality means the objects you compare
are to different instances of an object but happen to contain the same data.

Illustration (in java)

Date a = new Date(123);


Date b = new Date(123);
System.out.println(a==b); //false
System.out.println(a.equals(b)); //true

So a and b are different instances (different allocations in memory) but on the "data" level
they are equal.
Think about the words "identical" and "equivalent". If two things are identical, they have the
same identity; they are same thing. If they are equivalent, one can be substituted for the
other without affecting the outcome; they have the same behavior and properties.
For instance,

In StackOverFlow:

identity: I am Michael, you are sevugarajan, so we are not same.

equality: if we have same reputation scores, we are equal in some ways.


Identity: Two references to the same object ( o1 == o2).
Equality: The method o1.equals( o2 ) returns true. This doesn't necessarily mean that the
two objects contain (all) the same data.
In theory it's possible to override a method equals() to return false even for identical objects.
But this would break the specification of Object.equals():
The equals method implements an equivalence relation on non-null object references:

It is reflexive: for any non-null reference value x, x.equals(x) should return true.
It is symmetric: for any non-null reference values x and y, x.equals(y) should return
true if and only if y.equals(x) returns true.
It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true
and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any non-null reference values x and y, multiple invocations of
x.equals(y) consistently return true or consistently return false, provided no information
used in equals comparisons on the objects is modified.
For any non-null reference value x, x.equals(null) should return false.
Deep Equality Two objects O1 and O2 are deep equal if the
correspondent atomic object values are equal. (replacing the
identifiers with values) O1 : i1 O2 : i3 O3 : i4 a b b a a b 1 i2 1 1 i5 c
d c d Objects O1 and O2 are deep equal, and also O1 and O2, and
O2 and O3. Objects O1.b and O2.b are identical equal Objects O1
and O2 are shallow equal. Junping Sun Database Systems 7-31
Discussions on Object Equality Identical equality checks whether two
objects are the same. Shallow equality which goes one level deep,
checks the corresponding identifiers of the values of elements of the
objects. Deep equality checks the contents of the corresponding
base or atomic objects. Each object is (identical/shallow/deep) equal
to itself : reflexive. If O1 is (identical/shallow/deep) equal to O2, then
O2 is (identical/shallow/deep) equal to O1 : symmetric. If O1 is
(identical/shallow/deep) equal to O2, and O2 is
(identical/shallow/deep) equal to O3, then O1 is
(identical/shallow/deep) equal to O3 : transitive. The strongest
equality is identical, and shallow is stronger than deep. Two identical
objects are always shallow equal and deep equal, Two shallow
objects are always deep equal. Junping Sun Database Systems 7-32
Page 16

You might also like