JavaBeginner#java8

What is the difference between Optional.of() and Optional.ofNullable()?

Optional.of() throws a NullPointerException immediately if the value is null. Optional.ofNullable() safely wraps a possibly-null value, returning Optional.empty() if it's null.

Example
Optional.of(null); // throws NullPointerException
Optional<String> safe = Optional.ofNullable(null); // Optional.empty()

Related Questions

1
JavaIntermediate#java8#lambda

What is a method reference and what are its types?

Open
2
JavaIntermediate#streams

What is the difference between Stream.reduce() with and without an identity value?

Open
3
JavaAdvanced#streams

What is a parallel stream and when should you avoid it?

Open