views:

1158

answers:

11

What is the difference between identity and equality in OOP?

+1  A: 

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.

jitter
+9  A: 

basically,

  • 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 = new Integer(1);

a is equal but not identical to b.

Integer x = new Integer(1);
Integer y = x;

x is identical to y.

of course, two identical objects are always equal.

in java, equality is defined by the equals method. keep in mind, if you implement equals you must also implement hashCode.

Andreas Petersson
Actually, due to Integer pooling in Java 1.5+, in your example, a == b, so they are identical! Try it out on a 1.5 or higher JVM!
MetroidFan2002
this is wrong metroid. as noted in a different answer by me, the compile can an will not do tricks when allocating objects with "new" operator. only if you create them with Integer.valueof(1) the objects will be pooled.
Andreas Petersson
+1  A: 

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 ;-)

Antz
Is there a special case for Strings in Java? I thought that was just in .NET
finnw
strings are sometimes interned, for example when they are compile-time constants.
Andreas Petersson
+1  A: 

In Java and similar languages which 'leak' the abstraction of a reference of an object, you can test whether two references refer to the same object. If they refer to the same object, then the references are identical. In Java, this is the == operator.

There is also an equals method which is used to test whether two objects have the same value, for example when used as keys of a HashSet (the hash code of equal objects should also be equal). Equal objects should have the same 'value' and semantics when used by client code.

Purer object-oriented languages do not have an identity comparison, as client code generally shouldn't care whether or not two objects have the same memory address. If objects represent the same real-world entity, then that is better modelled using some ID or key value rather than identity, which then becomes part of the equals contract. Not relying on the memory address of the object to represent real-world identity simplifies caching and distributed behaviour, and suppressing == would remove a host of bugs in string comparison or some uses of boxing of primitives in Java.

Pete Kirkham
A: 

x == y is true only if there's the same object referenced by variables x and y.

x.equals(y) depends on the implementation of x.equals(), and is usually less strict that the above, as it compares the content of the object. (In Java, if x.equals(y), it must also be true that x.hashCode() == y.hashCode();)

Example:

Integer w = new Integer(3);
Integer x = new Integer(1);
Integer y = x;
Integer z = new Integer(1);

// all of these evaluate to true
y.equals(x) // it's the same object, of course the content is same
x.equals(z) // different objects, same content (`1`)
z.equals(y)
!w.equals(x); // the content is different (`3` vs `1`)
!w.equals(y);
!w.equals(z);
x == y // same object
z != x // different objects
y != z
w != x
Piskvor
A: 

For primitive types ( int , boolean , char, long , float ... )
== and != is equality test

and for Objects
== and != is identity test. [ it compares only the reference ]

equals method is used for equality test of Objects [ it can be overridden to compare specific attributes]

i found an excellent article on this http://www.cs.cornell.edu/courses/cs211/2006sp/Lectures/L14-Comparison/L14cs211sp06.pdf

http://ocw.mit.edu/NR/rdonlyres/Electrical-Engineering-and-Computer-Science/6-170Fall-2005/D659DC53-FB1D-403C-8E35-2CAECBED266E/0/lec12.pdf

Quote
I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals. :D
Sir Winston Churchill

Rakesh Juyal
A: 

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.
tangens
A: 

Identity concept is quite philosophical, that's why you shouldn't reconduce it just to references.

You can say that two identities are the same if a change to the first is reflected on the second and vice-versa. Of course this includes also sharing the same memory address but in general while identity is related to the attributes of the object, equality is used to check whenever two objects are identical, but this doesn't include identity.

The viceversa is quite obvious, if two items have the same identity they are also equal (in equality terms of being interchangeable).

Jack
+1  A: 

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.

outis
A: 

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.

卢声远 Shengyuan Lu