Differencebetween Identity and Equality in Oop

What is the difference between identity and equality in OOP?

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

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

Identity

For example:

Integer a = new Integer(1);
Integer b = a;

a is identical to b.

In Java, identity is tested with ==. For example, if( a == b ).

Equality

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.

What is the difference between == and equals() in Java?

In general, the answer to your question is "yes", but...

  • .equals(...) will only compare what it is written to compare, no more, no less.
  • If a class does not override the equals method, then it defaults to the equals(Object o) method of the closest parent class that has overridden this method.
  • If no parent classes have provided an override, then it defaults to the method from the ultimate parent class, Object, and so you're left with the Object#equals(Object o) method. Per the Object API this is the same as ==; that is, it returns true if and only if both variables refer to the same object, if their references are one and the same. Thus you will be testing for object equality and not functional equality.
  • Always remember to override hashCode if you override equals so as not to "break the contract". As per the API, the result returned from the hashCode() method for two objects must be the same if their equals methods show that they are equivalent. The converse is not necessarily true.

Difference between Equality & Object Equality

In Object Oriented Languages like Java, objects are data structures that contain both state and behavior. An object's state is determined by the value or values of it's internal data fields and its behavior is determined by the methods (functions or procedures) that are defined for the object (and which usually operate on the object's internal fields -- but not necessarily). Objects are always accessed by reference. That is, a variable holds a pointer to the memory location of the object.

Thus, objects may possess both value and location.

Equality tests whether two objects possess the same "value" (defined by the internal state of the objects), i.e. "do these two objects have the same value?"

Identity tests whether two -references- to an object are the same, i.e. "are these two objects the same object (at the same place in memory)?"

You'll notice that it is thus possible for objects to have the exact same value but different identities if they are separate structures in different memory locations. It is never possible for two object references to have the same identity (i.e., referring to the same data structure in memory) but different values.

What does object identity mean in java?

Suppose you have this simple class:

class Example {
private int value;

Example(int v) {
this.value = v;
}

public void showValue() {
System.out.println(this.value);
}
}

And we have this code (for instance, in a method somewhere else):

Example e1 = new Example(42);
Example e2 = new Example(42);

Then:

  • e1 and e2 have state (their value member). In this case, it happens both have the same state (42).

  • e1 and e2 have behavior: A method, showValue, that will dump out their value to the console. (Note that they don't necessarily have to have the same behavior: We could create a subclass of Example that did something different with showValue [perhaps showed it in a pop-up dialog box], and make e2 an instance of that subclass instead.)

  • e1 and e2 have identity: The expression e1 == e2 is false; they are not the same object. They each have a unique identity. They may be equivalent objects (we could implement equals and hashCode such that they were deemed equivalent), but they will never have the same identity.

No object ever has the same identity as another object; an object's identity is guaranteed unique within the memory of the running process.

(They also have other characteristics, such as their class type, but those are the main three.)

What's the difference between equal (=) and identical (==) in ocaml?

I don't know exactly how x.equals(y) works in Java. If it does a "deep" comparison, then the analogy is pretty close. One thing to be careful of is that physical equality is a slippery concept in OCaml (and functional languages in general). The compiler and runtime system are going to move values around, and may merge and unmerge pure (non-mutable) values at will. So you should only use == if you really know what you're doing. At some level, it requires familiarity with the implementation (which is something to avoid unless necessary).

The specific guarantees that OCaml makes for == are weak. Mutable values compare as physically equal in the way you would expect (i.e., if mutating one of the two will actually mutate the other also). But for non-mutable values, the only guarantee is that values that compare physically equal (==) will also compare as equal (=). Note that the converse is not true, as sepp2k points out for floating values.

In essence, what the language spec is telling you for non-mutable values is that you can use == as a quick check to decide if two non-mutable values are equal (=). If they compare physically equal, they are equal value-wise. If they don't compare physically equal, you don't know if they're equal value-wise. You still have to use = to decide.

Referential and structural equality in Kotlin

  • Referential equality === (also called identity) means that the pointers for two objects are the same. That is to say the objects are contained in the same memory location which leads us to the fact that pointers reference to the same object.

    identity: determines whether two objects share the same memory address

  • Structural equality ==, in its turn, means that two objects have equivalent content. You should specify when two objects should be considered equal by overriding the equals() method.

    equality: determines if two object contain the same state.

As well as in Java, in Kotlin there're no specific equals() and hashCode() generated by default (not considering data classes). Thus, until you've overriden these methods for your class, both == and === perform identity comparison.

vs .equals - why different behaviour?

In Java, you must use equals() for comparing equality between Strings. == tests for identity, a different concept.

Two objects can be equal but not identical; on the other hand if two objects are identical it's implied that they're equal.

Two objects are identical if they physically point to the same address in memory, whereas two objects are equal if they have the same value, as defined by the programmer in the equals() method. In general, you're more interested in finding out if two objects are equal.



Related Topics



Leave a reply



Submit