Java 8 - Optional
Java 8 - Optional In this blog, I’ll discuss about Optional class which was introduced in JDK8 under java.util.package. Optional class is used to represent a value is present or absent. With Optional class, developers have to less worry about NullPointerException and can work on neat and clean code. It contains at most one value. Advantages: · No need to check for null · No more NullPointerException at runtime Public final class Optional<T> extends Object { } Let’s understand all its usage with the help of example 1. Optional.empty() To create an empty Optional object we simply need to write // How to create empty Optional Optional<String> optional = Optional. empty (); System. out .println( "1. Optional: " + optional ); Output: 1. Optional: Optiona...