Chapter 11. Using Optional as a better alternative to null

This chapter covers

  • What’s wrong with null references and why you should avoid them
  • From null to Optional: rewriting your domain model in a null-safe way
  • Putting optionals to work: removing null checks from your code
  • Different ways to read the value possibly contained in an optional
  • Rethinking programming given potentially missing values

Raise your hand if you ever got a NullPointerException during your life as a Java developer. Keep it up if this Exception is the one you encounter most frequently. Unfortunately, we can’t see you at this moment, but we believe that there’s a high probability that your hand is raised now. We also guess that you may be thinking something like “Yes, ...

Get Modern Java in Action now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.