liveBook · Manning

Data type

As explained in Data-Oriented Programming in Java

A data type is a classification of data that determines its representation, permitted values, and valid operations.

A data type is not merely a container for values. It is a claim about what those values mean, which ones belong, and how the rest of the program may safely use them. A plain `double` can hold a contact angle, a dollar amount, or an unrelated calculation; a `Degrees` type says which of those meanings is intended and keeps that meaning visible as data moves through the program. section 2.3 section 3.2.3

Choose types by comparing the set of values they permit with the domain idea being modeled. An `int` denotes a broad range of integers, while a thermostat reading may be deliberately discrete and a measurement represented by `double` may require continuous precision. When most representable values are invalid for the domain, introduce a narrower type rather than relying on names, comments, or shared assumptions. section 2.3 section 3.3

Keep an angle an angle

A `Measurement` record can validate `contactAngleDegrees` during construction, but extracting that field produces an ordinary `double`; later code can accidentally combine it with dollars or pass it to an unrelated operation. Wrapping the value as `record Degrees(double value)` moves the valid range into the type and lets `Measurement`, collections, and operations continue to communicate that they carry degrees. section 3.2.2 section 3.3

Table 1.2 Comparing the meaning conveyed with different approaches
Old implicit variable assignments
Explicit concrete Data 
attempts+=1;
scheduledAt = small delta
class RetryImmediately {
    Instant scheduledAt;
    int attempts;
};
attempts=0;
scheduledAt=large delta;
class ReattemptLater {
    Instant scheduledAt;
);
attempts=0;
scheduledAt=null
class Abandon {
 
}

Compare vague state updates—such as a null schedule time—with named data types that make each retry outcome explicit. section 1.3.2

Named data types can also make states and decisions explicit. Instead of a `Status` record with a Boolean plus fields that are meaningful only sometimes, model `NotStarted`, `Completed`, and `Skipped` separately. An interface can unite those records as one `ChecklistStatus` while allowing each alternative to carry only the attributes that apply. This removes combinations that require defensive null checks. section 4.2.1 section 4.3.1

At a larger scale, types can tell the story of a workflow before its implementation exists: `List<Invoice>` becomes `List<PastDue>`, then a `LateFeeDraft`, then one of several explicit decisions. Representing branches as algebraic data types separates making a decision from acting on it, so different interpreters can handle the same decision later. section 5.3.1 section 5.3.2

Use a new data type when it earns its place by making a domain distinction durable:

  • Wrap values such as degrees, currency, coordinates, or percentages when their units, ranges, or operations differ from the underlying primitive. section 3.3 section 3.4.1
  • Name each meaningful outcome of a conditional when an `else` branch hides a real domain case. section 6.3.6
  • Model mutually exclusive lifecycle states as distinct alternatives instead of optional fields and flags. section 4.3.1 section 5.4.5
  • Stop adding types when their safety and clarity no longer outweigh their boilerplate and ergonomic cost. section 3.4 section 5.4

More types are not automatically better. Under-typed models leave room for misunderstandings and illegal states; over-typing can make ordinary work cumbersome. Aim for representations that balance semantic clarity, safety, and ease of use. The goal is not purity: it is to make the wrong thing difficult—or impossible—to express where the domain stakes justify it. section 3.4 section 3.7

How Data-Oriented Programming in Java covers it

How deep it goes
Develops representation from primitive choices through validated wrappers, algebraic alternatives, and compile-time lifecycle constraints.
Examples
Illustrates retry decisions, temperature precision, contact-angle degrees, checklist statuses, percentages, and late-fee lifecycles.
What you can do after
Apply it via Java types to encode domain constraints, preserve meanings, and eliminate invalid states.
How it connects
Connects data types with invariants, records, interfaces, algebraic data types, pattern matching, and domain modeling.

Also explained in

Compare how 22 books explain data type →

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.