When using the Java 8 Optional class, there are two ways in which a value can be wrapped in an optional.
Optional
String foobar = ;
In addition, If you know your code should not work if object is null, you can throw exception by using Optional.orElseThrow
Optional.orElseThrow
String nullName = null; String name = Optional.ofNullable(nullName).orElseThrow(NullPointerException::new);