Chapter 2: Data, Identity, and Values
Introduces the chapter’s plan to explore representing data as data in Java, including entities that change over time.
After this chapter you will be able to
- Identity Across Changing States. Explain how a variable or object can retain its identity while the state associated with it changes over time.
- Identity Beyond Java Objects. Distinguish identity itself from the variables, objects, or databases used to track it, and state how the section says data-oriented programming can model identity.
- Values and immutable state. Explain that a value is a single, immutable state with no identity, so it cannot change over time.
- Java object identity versus value. Distinguish the object identity Java gives a value-class instance from the state that determines the value it represents.
- Equal state and substitutability. Explain why value objects with the same state represent the same value and can be substituted without changing a program's meaning or behavior.
- Complexity of Value Classes. Explain why a value class need not be small or simple, and why object-oriented judgments about large classes without behavior do not necessarily apply to values.
- Value semantics for collections. Explain how a Java collection can behave as a value: it must not change, and its equality must reflect its contents, which must themselves be values. Describe how unmodifiable collections provide the no-change part.
- Mutable holders and identity. Explain why assigning a value to a mutable variable does not make the variable itself a value: the variable can hold different values over time.
- Final as value communication. Describe how `final` helps preserve and communicate intended value semantics for variables and class fields, rather than leaving that intention only in developers’ heads.
- Mutable References and Value Semantics. Explain why a value object must be made of values throughout, and why an unmodifiable list does not restore value semantics if it contains an object with a mutable reference.
- Values With Domain Meaning. Explain why a value such as 4 becomes data when it is about something, such as a temperature or a sales total.
- Domain Meaning And Code Meaning. Distinguish the meaning of domain data from the meaning expressed by code, and explain why modeling requires understanding the domain first.
- Numeric Types Communicate Fidelity. Describe how choosing an integer or a double for temperature data communicates different expectations about measurement fidelity.
- Range of data representations. Explain that data is not limited to records of events: it can represent abstract ideas such as decisions and actions, or even consist of a meaningful name alone. State that data can also be about an identity and things that change over time.
- Identity Beyond Java Objects. Explain why an object's identity is not tied to a particular Java object, machine, or language construct.
- Change As Successive Values. Describe how a sequence of separate values can represent change without modifying an object in place.
- Change through successive values. Explain how a program represents change without modifying data: each state is a value, and a function takes the current state and returns the next one.
- Records as transparent value carriers. Explain why records are more than a way to reduce boilerplate: they are intended to carry values transparently, with their state specified by the attributes in the header rather than hidden behind a separate representation.
- Record state and value identity. Describe how a record is determined by the state supplied at construction: it is effectively immutable, and its Java object identity is not semantically meaningful when comparing the values it represents.
- Semantic invariant validation. Explain why compact constructors validate record arguments and what kind of validation belongs there.
- Incoming argument transformation. Describe how a compact constructor can transform arguments before assignment and why the section highlights mutable references.
- Records and mutable references. Explain why a record is not necessarily an immutable value when one of its components refers to a mutable Java collection.
- Defensive copying during construction. Describe how making a copy of an incoming list during construction protects a record’s value semantics.
- Explicit immutable list types. Explain why a small record wrapper for an immutable list communicates the design more clearly than using the general List type alone.
- Nulls and data meaning. Explain why nulls undermine a data-oriented representation and why a domain value that is available only sometimes should be modeled as explicitly optional.
- Constructor null-check tradeoff. Describe why constructor validation could reject nulls and why the author does not generally require explicit null checks in records.
- Named local data types. Explain why a locally defined record can be clearer than a tuple or other positional structure when a method needs to group a few pieces of data.
- Related record code layout. Explain why records can make it reasonable to place related data types together in one file rather than follow the usual one-class-per-file layout.
- Value Modeling Without Records. Explain how to model data on an older JDK without records, and why doing so is still practical despite the extra code.
- Annotation Processor Alternatives. Describe how Lombok and Immutables reduce the work of modeling data on older Java versions, and distinguish how they produce their results.
- Identity and value distinction. Distinguish identity from value: identity is a modeling choice, while a value has no identity and cannot change.
- Meaning turns values into data. Explain how giving a value domain meaning makes it data, using the section's example of the integer 4.
Section by section
Introduction
Introduces the chapter’s plan to explore representing data as data in Java, including entities that change over time.
2.1 Identity is about continuity over time
The section explains identity as continuity across changing states, rather than a particular state or the Java mechanism used to track it.
- Identity Across Changing States: Explain how a variable or object can retain its identity while the state associated with it changes over time.
Why it matters: It helps you distinguish a customer or variable from its changing attributes or stored value. - Identity Beyond Java Objects: Distinguish identity itself from the variables, objects, or databases used to track it, and state how the section says data-oriented programming can model identity.
Why it matters: It prevents you from assuming that representing identity requires an identity-bearing Java object.
Key terms: identity, state, object identity, values, data-oriented programming
2.2 Representing Values in Java
Defines values and explains how Java classes can represent them despite Java objects having identities.
- Values and immutable state: Explain that a value is a single, immutable state with no identity, so it cannot change over time.
Why it matters: This definition distinguishes a value from a thing whose identity persists while its state changes. - Java object identity versus value: Distinguish the object identity Java gives a value-class instance from the state that determines the value it represents.
Why it matters: It prevents Java's object machinery from being mistaken for a property of the value being modeled. - Equal state and substitutability: Explain why value objects with the same state represent the same value and can be substituted without changing a program's meaning or behavior.
Why it matters: It gives a practical test of whether a Java class is behaving like a value.
Key terms: value, immutable state, identity, Value Classes, object identities, value objects, state, same state, same value, equivalent value object
2.2.1 Building complex values out of other values
This section argues that value classes can be as large and complex as the domain requires, despite object-oriented instincts to treat them as simple or “anemic.”
- Complexity of Value Classes: Explain why a value class need not be small or simple, and why object-oriented judgments about large classes without behavior do not necessarily apply to values.
Why it matters: It lets you model substantial information as a value without rejecting the design solely because it looks unusual in object-oriented code.
Key terms: value objects, value classes, object identity, anemic
2.2.2 Modeling Java collections as values
This section explains when Java collections can behave as values rather than identities, and why collection constructors can undermine that behavior.
- Value semantics for collections: Explain how a Java collection can behave as a value: it must not change, and its equality must reflect its contents, which must themselves be values. Describe how unmodifiable collections provide the no-change part.
Why it matters: It lets you decide whether a collection should be treated as a stable value or a changing object. - Collection creation and mutability traps: Describe the collection-creation options given in the section and explain why a constructor such as Arrays.asList(...) can produce different semantics than expected.
Why it matters: A collection that looks fixed may still allow changes that defeat the intended value semantics.
Key terms: value object, identity object, Unmodifiable wrappers, equality, unmodifiable, Collections package, .of(), Arrays.asList(...), internal mutability
2.2.3 Things to watch out for when creating values
This section explains how mutable variables can undermine intended value semantics and how `final` helps make those semantics clear in Java.
- Mutable holders and identity: Explain why assigning a value to a mutable variable does not make the variable itself a value: the variable can hold different values over time.
Why it matters: It helps you distinguish an immutable value from a mutable name that currently holds it. - Final as value communication: Describe how `final` helps preserve and communicate intended value semantics for variables and class fields, rather than leaving that intention only in developers’ heads.
Why it matters: It makes value-oriented intentions visible to future readers and harder to undermine accidentally. - Practical use of final: Explain the author’s qualified advice on using `final`: it is useful for establishing value-oriented habits, but the examples omit it for brevity.
Why it matters: It prevents you from mistaking the book’s terse examples for a claim that `final` has no value.
Key terms: value, identity, mutable variable, final, value semantics, instance variables, data-oriented patterns
2.2.4 Mutability converts values back into identities
This section explains why a mutable reference anywhere inside a value object makes it behave like an identity, even when enclosed in an unmodifiable collection.
- Mutable References and Value Semantics: Explain why a value object must be made of values throughout, and why an unmodifiable list does not restore value semantics if it contains an object with a mutable reference.
Why it matters: An object that looks like a value can behave unexpectedly if anything inside it is mutable.
Key terms: value objects, mutable reference, identity, Unmodifiable
2.3 Data = values + meaning
The section explains how a value becomes data when it has domain meaning, and how representation communicates that meaning in code.
- Values With Domain Meaning: Explain why a value such as 4 becomes data when it is about something, such as a temperature or a sales total.
Why it matters: The same value can mean very different things depending on what it describes. - Domain Meaning And Code Meaning: Distinguish the meaning of domain data from the meaning expressed by code, and explain why modeling requires understanding the domain first.
Why it matters: A code representation cannot communicate domain meaning well until that meaning has been understood. - Numeric Types Communicate Fidelity: Describe how choosing an integer or a double for temperature data communicates different expectations about measurement fidelity.
Why it matters: A type choice tells readers something about which distinctions the program considers important.
Key terms: values, data, semantics, semantics of the data, semantics of our code, domain, representation, integers, double, fidelity
2.3.1 What can data be?
The section broadens data from information about events to meaningful values that can represent actions, decisions, names, and even identities.
- Range of data representations: Explain that data is not limited to records of events: it can represent abstract ideas such as decisions and actions, or even consist of a meaningful name alone. State that data can also be about an identity and things that change over time.
Why it matters: This wider view lets you recognize data in representations of a program’s ideas, not just its reports and records.
Key terms: data, value, meaning, decisions, actions, identity
2.4 Identity in the World of Data
The section shows that identity is continuity across a succession of values, not something that requires a particular Java object or in-place mutation.
- Identity Beyond Java Objects: Explain why an object's identity is not tied to a particular Java object, machine, or language construct.
Why it matters: It lets you distinguish the thing being modeled from the object used to model it. - Change As Successive Values: Describe how a sequence of separate values can represent change without modifying an object in place.
Why it matters: It shows why modeling change with values does not require giving up continuity. - Imposed And Realized Identity: Explain how programmers relate successive values to one identity and where the modeled identity is realized.
Why it matters: It clarifies why values can model one continuing thing without carrying identity intrinsically.
Key terms: identity, succession of values, object identities, values, frames, timeline, mutation, side-effected
2.5 Programming with immutable data
The section shows how immutable data can represent change through successive values and compares two ways to write the operations that produce them.
- Change through successive values: Explain how a program represents change without modifying data: each state is a value, and a function takes the current state and returns the next one.
Why it matters: It explains how immutable data can still model something that changes over time. - Two placements for state operations: Distinguish a stand-alone method that takes a value explicitly from a method on a value object that uses its state implicitly, while explaining why both remain data processing.
Why it matters: A method on a value can look object-oriented while still computing a new value rather than mutating an object. - Chaining and style tradeoffs: Describe how methods returning new versions of a value allow readable chaining, and state the section’s broad preference for attached methods in math-like operations versus separate operations for general business data.
Why it matters: It helps you choose a readable style without mistaking that style for a requirement of immutable programming.
Key terms: succession of values, current state, next state, stand-alone static method, value object, data in; data out, fluent-like APIs, chaining, domain behaviors
2.6 Modeling Data and Values with Records
The section introduces Java records as transparent, concise carriers of values whose meaning comes from their declared state rather than object identity.
- Records as transparent value carriers: Explain why records are more than a way to reduce boilerplate: they are intended to carry values transparently, with their state specified by the attributes in the header rather than hidden behind a separate representation.
Why it matters: This explains why choosing a record is a modeling decision, not just a shortcut for writing less code. - Record state and value identity: Describe how a record is determined by the state supplied at construction: it is effectively immutable, and its Java object identity is not semantically meaningful when comparing the values it represents.
Why it matters: It distinguishes a record’s value-oriented meaning from the identity every Java object technically possesses.
Key terms: records, transparent carriers of values, information hiding, state description, effectively immutable, object identity, state, value objects
2.6.1 Compact Constructors
This section explains how record compact constructors reduce constructor boilerplate, enforce a data type’s invariants, and transform incoming arguments before assignment.
- Compact constructor defaults: Explain what a compact constructor lets a Java programmer omit and what happens to the record’s arguments.
Why it matters: It leaves the constructor focused on the checks and transformations that matter. - Semantic invariant validation: Explain why compact constructors validate record arguments and what kind of validation belongs there.
Why it matters: Checks at construction keep invalid data from circulating and show other programmers how the type is meant to be used. - Incoming argument transformation: Describe how a compact constructor can transform arguments before assignment and why the section highlights mutable references.
Why it matters: Changing inputs before assignment can keep an unexpected mutable reference from undermining a record intended to represent a value.
Key terms: Compact Constructor, record’s header, validation, invariants, semantics, transform, incoming arguments, mutable references
2.6.2 Things to watch out for with records (Part I): shallow immutability
This section explains how mutable collection references can undermine a record’s value semantics, and how copying and explicit types help preserve them.
- Records and mutable references: Explain why a record is not necessarily an immutable value when one of its components refers to a mutable Java collection.
Why it matters: A record declaration alone does not guarantee that the data it represents will stay unchanged. - Defensive copying during construction: Describe how making a copy of an incoming list during construction protects a record’s value semantics.
Why it matters: The record can defend its intended semantics even when callers supply a mutable collection. - Explicit immutable list types: Explain why a small record wrapper for an immutable list communicates the design more clearly than using the general List type alone.
Why it matters: The type can tell readers that the design calls for an immutable value rather than leaving that intention hidden in implementation code.
Key terms: records, value-like, mutable reference, List, transformation in the constructor, copy, value semantics, mutability, wrapper types, ImmutableList
2.6.3 Things to watch out for with records (part II): nulls
This section explains why nulls undermine trustworthy data models and why explicit null checks in record constructors are a design tradeoff.
- Nulls and data meaning: Explain why nulls undermine a data-oriented representation and why a domain value that is available only sometimes should be modeled as explicitly optional.
Why it matters: Explicit optionality tells readers of the model something meaningful that a null does not. - Constructor null-check tradeoff: Describe why constructor validation could reject nulls and why the author does not generally require explicit null checks in records.
Why it matters: It helps you distinguish a possible safeguard from a rule the author insists every record must follow.
Key terms: null, optionality, data model, constructor validation, semantic validation, boilerplate
2.6.4 Some other niceties that come with records
This section shows how records make small, named data types easy to define, improving readability within methods and changing how related types can be laid out in files.
- Named local data types: Explain why a locally defined record can be clearer than a tuple or other positional structure when a method needs to group a few pieces of data.
Why it matters: Low-cost, explicit names make small groups of data easier to read. - Related record code layout: Explain why records can make it reasonable to place related data types together in one file rather than follow the usual one-class-per-file layout.
Why it matters: Seeing related types together can make a program's data model easier to grasp.
Key terms: local records, tuples, positional based data structures, records, one class per file, related data types
2.7 What to do if you’re on an older JDK
Older Java versions can still support data-oriented modeling through ordinary classes or annotation processors that reduce boilerplate.
- Value Modeling Without Records: Explain how to model data on an older JDK without records, and why doing so is still practical despite the extra code.
Why it matters: You can apply the book’s approach even when your project cannot upgrade its JDK. - Annotation Processor Alternatives: Describe how Lombok and Immutables reduce the work of modeling data on older Java versions, and distinguish how they produce their results.
Why it matters: These tools offer record-like data modeling with less handwritten boilerplate on older JDKs.
Key terms: vanilla classes, records, boilerplate, annotation processor, Lombok, Immutables, byte code manipulation, source generation
2.8 Wrapping up
The section recaps identity, values, data, and the Java tools used to represent them.
- Identity and value distinction: Distinguish identity from value: identity is a modeling choice, while a value has no identity and cannot change.
Why it matters: This distinction helps you decide what your program should treat as an enduring thing and what it should treat as a value. - Meaning turns values into data: Explain how giving a value domain meaning makes it data, using the section's example of the integer 4.
Why it matters: The meaning attached to a value is what lets it model something in the domain. - Java value and data representations: Describe how the section characterizes Java value classes and records, including the role of compact constructors.
Why it matters: These Java constructs provide a way to express values and meaningful data while checking inputs at construction.
Key terms: identity, values, data, semantics, domain, value classes, records, compact constructor
2.9 Summary
Where this is used later
- The importance of understanding meaning (chapter 3) · Develops the importance of understanding meaning when modeling a domain.
- Scoping numeric types to specific ranges (chapter 3) · Examines more specific choices about numeric types.
- Functions, Purity, and Determinism (chapter 6) · The discussion of functions and determinism builds on computing outputs from inputs.
- Noticing algebraic operations in every day code (chapter 7) · Assumes familiarity with programming using immutable data.
- Work backwards from invariants (chapter 9) · The use of immutable data to avoid changing state.
- Write utilities that make data-oriented programs more expressive (chapter 9) · Programming with immutable data
- Modeling domain behaviors around data (chapter 5) · Separating operations from data is revisited when modeling domain behaviors.
- Records are tools for modelling “AND” (chapter 4) · Uses records to model data with declared components.
Concepts explained in this chapter
- identityObject identity is the property that distinguishes an object from other objects regardless of its current data values.
- value objectA value object is an object that represents an immutable value and is distinguished by equality of its properties rather than by object identity.
- valueA value is information whose meaning is determined by its contents rather than by the identity of the object carrying it.
- value semanticsValue semantics are rules under which an entity is defined by stable descriptive values rather than by changeable state or identity.
- semanticsSemantics are the intended meanings that a program expresses through its data and operations.
- stateThe data values that describe the current condition of an object or entity.
- mutable referenceA mutable reference is a reference that provides access to an object whose state can be changed.
- dataData is information represented independently of the behaviors that operate on it.
- unmodifiable collectionAn unmodifiable list is a list whose exposed operations do not permit modification of its contents.
- mutationMutation is the modification of an existing program value or state in place.
- compact constructorA compact constructor is a Java record constructor form that omits the explicit parameter list and field assignments while allowing constructor-body validation and transformation of component values.
- data as dataData as data is the representation of information as ordinary, openly accessible values rather than as hidden private object state.
- shallow immutabilityShallow immutability is a condition in which an object's own fields cannot be changed while objects reachable through its references may still be mutable.
- wrapperA wrapper is a type or software component that encloses another representation or interface and exposes a deliberately shaped semantic interface around it.
- fluent interfaceFluent chaining is an API style in which successive operations are linked through return values to form an expression-like sequence.
- functionA computation that maps input values to an output value or values without requiring mutation of the input values.
- data transformationData transformation is the process of producing successive data states by changing or converting an existing representation.
- data typeA data type is a classification of data that determines its representation, permitted values, and valid operations.
- data-oriented programmingData-oriented programming is a programming approach that organizes software around explicit data representations and operations on those representations.
- mutable objectMutability is the property of a value or object whose state can change after it is created.
- missing valueA special Java reference value indicating the absence of an object rather than an instance of the referenced type.
- optionalityThe property of domain information that may be present or absent and whose absence has explicit meaning.
- invariantAn invariant is a condition that must remain true across the valid states of a data structure or computation.
- domain semanticsDomain semantics are the meanings and validity rules that a software model assigns to concepts in its problem domain.
- unit of measurementA unit is a defined reference quantity used to express and compare measurements of a property.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
