liveBook · Manning

Data type

As explained in The Art of Code

A type is a specification of the values a program entity can represent and the operations permitted on those values.

A data type specifies which values a program entity can represent and which operations those values permit. Choosing the right type is part of making code communicate intent, not merely satisfying syntax. section 2.4

Use a type that fits the domain. Representing monetary values with BigDecimal rather than float preserves numerical precision; poor choices can later affect performance, memory usage, concurrency, or correctness. section 2.4

Types make a method’s cast believable

A promotional-email method uses long for a user identifier, User for the loaded person, boolean for a birthday condition, List<Promotion> for available promotions, and String for the generated content. These types make each role visible and help the method read as a sequence of meaningful actions. section 2.4

Listing 2.6 Scene-level method building promotional email content
public String buildPromotionalEmailContent(long userId) {
  String emailContent;          #1
  User user = userService.findUserById(userId);    #2
  boolean isUserBirthday = isToday(user.getBirthday());

  if (isUserBirthday) {
    List<Promotion> birthdayPromotions =                                    #3
      promotionService.getPromotionsFor(user);                    #3         
    emailContent = birthdayPromotionEmailFor(user, birthdayPromotions);
  } else {
    List<Promotion> standardPromotions =                     #3              
      promotionService.getPromotionsFor(user);                     #3       
    emailContent = regularPromotionEmailFor(user, standardPromotions);
  }
  return emailContent;
}

Inspect the deliberately noisy version: separate String, User, boolean, and List variables show how type choices appear inside a concrete method before refactoring. section 2.4

Listing 2.7 Improved scene-level method for building promotional email content
public String buildPromotionalEmailContent(long userId) {
  User user = userService.findUserById(userId);    #1
  List<Promotion> promotions = promotionService
     .getPromotionsFor(user);    #2

  if (isToday(user.getBirthday())) {     #3
    return birthdayPromotionEmailFor(user, 
      promotions);                             #4
  }
  return regularPromotionEmailFor(user,       #4
    promotions);                              #4
}

Compare the refactored version, where User, List<Promotion>, boolean, and String remain explicit while unnecessary temporary state is removed. section 2.4

How The Art of Code covers it

How deep it goes
Connects type choice to precision, performance, memory usage, and concurrency, but stops short of explaining type-system internals.
Examples
Uses BigDecimal versus float for prices and typed Java-style variables in a promotional-email method.
What you can do after
Evaluate and choose types that represent domain values accurately and clarify each variable’s responsibility.
How it connects
Connects data types with readability, numerical precision, performance, memory usage, concurrency, and domain modeling.

Also explained in

Compare how 18 books explain data type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Art of Code. The section links point to the parts of the book it draws on.