Identity gives continuity to a related succession of states over time. A customer can change email, balance, or premium status while remaining the same customer; its identity is not any one current attribute. section 2.1
In Java, objects and mutable variables commonly act as stable places that hold changing values. That mechanism is useful, but it is not identity itself: identity can outlast a particular object, database, machine, or address space. section 2.1 section 2.4
A perfect copy is still a different object
Two Customer instances can contain identical account IDs, timestamps, emails, and flags, yet remain distinct object identities. This distinction matters when an object represents an entity whose state is expected to change over time. section 2.1
Values take the opposite approach. A value is immutable state with no semantically meaningful identity: equal state means the same value, regardless of which object happens to hold it. Comparing value objects by object identity can therefore produce misleading results. section 2.2
When modeling with values, make the intended semantics explicit:
- Use immutable state and equality based on that state for value objects. section 2.2
- Use final assignments and fields to prevent a holder from tracking different values over time. section 2.2.3
- Represent an evolving entity as a succession of immutable values when continuity need not depend on mutation in place. section 2.4
Listing 2.1 A relationship between state and time
public static void main(String... args) { |
double xPosition = 4.2; |
|
xPosition++; |(time) #A
xPosition++; | #A
xPosition *= xPosition; ▼ #A
}A changing position variable makes the separation clear: the stored number changes, while the named location supplies continuity across those changes. section 2.1
Data can describe identity and change without being a mutable object. A sequence of states, played back in order, can model the continuing life of an entity while each individual state remains plain immutable data. section 2.3.1 section 2.4








