liveBook · Manning

Identity

As explained in Data-Oriented Programming in Java

Object identity is the property that distinguishes an object from other objects regardless of its current data values.

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

How Data-Oriented Programming in Java covers it

How deep it goes
Defines continuity through time, contrasts objects and values, and develops succession-of-values modeling.
Examples
Uses changing positions, Customer copies, Vector equality, cached Integers, and a simulated ball.
What it assumes
Builds from Java variables and objects before introducing value-oriented alternatives.
What you can do after
Evaluate/understand whether a domain model needs continuity over time or state-based value semantics.
How it connects
Connects object identity with variables, values, value objects, immutability, data, records, and data-oriented programming.

Also explained in

Compare how 9 books explain identity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.