Optional.of() vs Optional.ofNullable()
Fast recall
Quick revision
Core answer
Practical Optional API knowledge—understanding the contract and safety guarantees of creation methods.
Using Optional.of on a potentially null value, causing immediate NullPointerException and defeating Optional's purpose.
Explain when you'd intentionally use Optional.of—to document a programming error if null appears where it shouldn't.
Interview-ready guide
Interview answer
The difference is in how they handle null.
Optional dot of throws NullPointerException immediately if you pass it null. It's saying this value must not be null, and if it is, that's a bug we want to catch right away. You'd use of when you're certain the value exists, like after you've already checked it or when you're wrapping something from an API that guarantees non-null.
It's a fail-fast mechanism. On the other hand, Optional dot ofNullable accepts null gracefully. If you pass it null, it just returns Optional dot empty.
This is what you use when you're wrapping a value that might be null, which is most of the time. Like if you're getting a result from a database query or a map lookup, you don't know if it exists, so you use ofNullable. The common mistake is using Optional dot of on something that might be null, like the result of map dot get.
That immediately throws NPE, which defeats the whole point of using Optional. You should use ofNullable there. But there are cases where of is actually better.
If you're deep inside your business logic and you know a value must exist at that point, using of enforces that assumption. If someone breaks that assumption later, you get an immediate NPE instead of a silent empty Optional that causes problems downstream. So use ofNullable at boundaries where null is possible, and of when you're documenting a guarantee.
Optional.of() vs Optional.ofNullable()
Direct Answer
Optional.of(value) throws NullPointerException immediately if the value is null, enforcing that the value must be non-null. Optional.ofNullable(value) accepts null and returns Optional.empty() in that case. Use ofNullable when wrapping potentially null values from external sources. Use of only when you're certain the value exists and want fail-fast behavior.
Common Mistakes & Pitfalls
- Using Optional.of on potentially null values: writing Optional.of(map.get(key)) throws NPE immediately when the key is missing—defeats Optional's purpose. Should be Optional.ofNullable.
- Using ofNullable everywhere: writing Optional.ofNullable(userId) when userId is guaranteed to exist from a validated request adds unnecessary null check—use of to document the guarantee.
- Not using empty: writing return Optional.ofNullable(null) to return an empty Optional instead of just return Optional.empty()—less clear intent.
- Wrapping Optional in Optional: calling Optional.of(someOptional) instead of just returning the Optional directly.
Check your understanding
Answer these without looking back at the article.
- When is it appropriate to use Optional.of() instead of ofNullable()?
- What does Optional.empty() return?
- Is Optional.of(Optional.empty()) valid Java?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.