chapter two

2 Data, Identity, and Values

 

This chapter covers

  • Identity and change
  • Values and Value Objects
  • What “Data as Data” means
  • Modeling data with Records

This chapter is about how to represent data "as data" in a language where everything is an object. It’s easy once you know the trick, but to get there we first have to dive into what objects are, pick apart how they model time and space, and explore casual metaphysical questions like “what does it mean to be something?”

By the end, we’ll know not just how to model static information “as data,” but how to model complex, real-world entities that grow and change over time.

2.1 Identity is about continuity over time

Identity is state + time. Even something as simple as a variable assignment sets up a deep relationship between the identity of the variable and the states that get stored inside it over time.

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
}

2.2 Representing Values in Java

2.2.1 Building complex values out of other values

2.2.2 Modeling Java collections as values

2.2.3 Things to watch out for when creating values

2.2.4 Mutability converts values back into identities

2.3 Data = values + meaning

2.3.1 What can data be?

2.4 Identity in the World of Data

2.5 Programming with immutable data

2.6 Modeling Data and Values with Records

2.6.1 Compact Constructors

2.6.2 Things to watch out for with records (Part I): shallow immutability

2.6.3 Things to watch out for with records (part II): nulls

2.6.4 Some other niceties that come with records

2.7 What to do if you’re on a JDK older than 17

2.8 Wrapping up

2.9 Summary