PrepZone Logo
PrepZone
Back to Java

Java · Easy

What is Optional in Java? How does it help avoid NullPointerException?

JavaJava 8+

Answer preview

Optional<T> — is a container that may or may not hold a non-null value. It was introduced in Java 8 to explicitly model the absence of a value instead of returning null.…