Reference vs. Value Equality
[comp1110-1T]
Reference vs. Value Equality
[comp1110-1T]The difference between value types and reference types explains the differences between == and other methods to decide equality.
For value types, == happens to capture regular equality - for example, there are no two int values that represent the number 32 but are somehow different.
On the other hand, for reference types, == describes reference equality, identifying whether two references point to the same object in memory.
Since there may be two different objects representing the same value, == is too strict in many cases.
We have seen this for String equality and Array equality.
It is the same way in general for objects of arbitrary classes, except that for those, by default, no other notion of equality comparison exists.
We'll see later how to define our own equality comparison.
Finally, Enums are a special case: they are a reference type, but with a guarantee that each case of a Enum refers to the same object in memory.
As such, comparing the references to Enum values using == is fine - as for ints, there are no two distinct values representing the same case.