Optional in Java
Use Optional to make "there may be no value" explicit instead of returning null: creating Optionals, orElse vs orElseGet, map and flatMap, ifPresentOrElse, where Optional belongs and where it does not, and the anti-patterns to avoid.
CodexBash, , 3 min read
NullPointerException is the most common runtime error in Java, usually because a method returned null and the caller did not expect it. Java 8 introduced Optional: a small container that either holds a value or is empty, so a method's signature itself says "this might not find anything". Used well, it removes a whole category of bugs; used everywhere, it adds clutter. This article covers both sides.
Sign in to read the rest
The whole 3 minute article is free with an account, along with every problem, lesson and tutorial. No card.