Abstract
Important
Optional
focuses on data presence, while exceptions handle operational issues, such as database connectivity. By returningOptional.empty()
for missing data, the caller knows it’s not an error, it’s simply an empty result. WithoutOptional
, it’s unclear whethernull
means “no data” or “method failed.” WithOptional
, the intent becomes explicit.Java Optional is a Maybe Monad.
Attention
We shouldn’t overuse
Optional
. It should be used as a return type to indicate whether the returned object contains a value or is empty. Avoid using it as a parameter!