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

















