O'Reilly logo

Learning Java Functional Programming by Richard M Reese

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Optional and Monads

In functional languages, there is a data type called the option type, also called the maybe type. Its purpose is to encapsulate a value, which is returned from a function. This option return type may indicate that there is no return value. The problem with returning a value such as zero or null is that these may be legitimate return values. With an option type, we can return a good value or indicate that nothing is returned.

In Java 8, the Optional class has been added to serve as an option type. It is used to wrap an object and possesses a number of methods that can reflect a non-value and deal with these values. The calls allow us to handle null values and avoid the dreaded null pointer exception. The Optional

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required