Optional is implemented in JDK since JDK 8.
It humbly wants to avoid using the checks of null throught the code (just as you actually did)
Optional
return here means we can either have an ApplicationType
value or null
.
When having an Optional
you can check the value with isPresent()
. The value of Optional
can be fetched with get()
Optional are interesting especially when performing action directy with functions on the optinal itself with something alone with this :
Optional<ApplicationType> applicationType = repo.findById(1L);
applicationType.ifPresent(value -> doSomething(value));
applicationType.orElse(defaultApplicationType);
Here a complete guide on Optionals