Lets understand the use of Optional with a small real example in a production code to understand the real power of Optional

Suppose you want to get the value from an environment variables and want to set a default value if value is not present instead of throwing NullPointer Exception. In a traditional way we would write

private static String getPropertyOrThrow(String propertyName) throws IllegalArgumentException {
    String value = System.getProperty(propertyName);
    if (value == null) {
        value = System.getenv(propertyName);
    }
    if (value != null) {
        return value;
    }
    throw new IllegalArgumentException("`" + propertyName + "` property not defined.");
}

With Optional we can write the same code as follows

public static String getPropertyOrDefault(String propertyName, String defaultValue) {
    return Optional.ofNullable(System.getProperty(propertyName)).orElse(defaultValue);
}

See how easy it is to write the above code in just 1 line. Looking at this now you might have one question

What is Optional ?

Java 8 introduced a new class java.util.Optional which encapsulates a value. A container object which may or may not contain a non-null value. If a value is present, isPresent() will return true and get() will return the value. It prevents any NullPointerExceptions runtime and helps us build clean and tidy Java APIs or Applications. Like Collections and arrays, it is also a Container to hold at most one value.

Different ways to create Optional are:

  1. Optional.empty() – This method will return an empty Optional object.
  2. Optional card = Optional.empty();
  3. Optional.of() – This method will return an Optional of object passed as an argument to the of method. Returns an Optional with the specified present non-null value.
  4. Optional value = Optional.of(System.getProperty(propertyName));
  5. Optional. ofNullable() – Returns an Optional describing the specified value, if non-null, otherwise returns an empty Optional
  6. Optional value = Optional.ofNullable(System.getProperty(propertyName));;

Difference between .of and .ofNullable ?

Optional.of() will return the object of Optional without null check. It means if there is no value present it will return null. The method to define .of() is

public static <T> Optional<T> of(T var0) {
    return new Optional(var0);
}

Optional.ofNullable() will return the object of Optional with a null check. If this method gets the null as an input then it will return the empty Optional otherwise it returns an Optional with the specified present non-null value. The method to define .ofNullable() is

public static <T> Optional<T> ofNullable(T var0) {
    return var0 == null ? empty() : of(var0);
}

Optional.of() will be used when you are confident the Optional will never have a null object and it will either contain the value of the object or it will be empty but not null. Optional.of() can also throw a NullPointerEception if a null value for the Optional is generated.

Optional.ofNullable()- is used in situations where the value is likely to be null.

#scala #api #java8

Java8 Optional API.What is Optional ?
3.95 GEEK