Many a programmer has been bitten by NullPointerException. While occurring less in Java than C or C++, it may still occur. Now, Java has the idea of an Optional field. An Optional field may or may not contain a value. You can test whether there is a value present or not rather than the awkward null tests that exist.
We can run through several aspects of Optional by using the following code snippet:
import java.util.Optional; public class MyOptional { public static void main() { MyOptional program = new MyOptional(); Integer value1 = null; Integer value2 = 123; //.ofNullable allows null Optional<Integer> a = Optional.ofNullable(value1); //.of does not allow null Optional<Integer> b = Optional.of(value2); System.out.println(program.sum(a,b)); } ...