Skip to main content
Java Code Completion Medium +20 XP

Optional Chaining

Complete to safely get value from Optional.

JavaFill in the blank
Optional<String> name = Optional.of("Alice");
String result = name.___("Unknown");

Type the missing code

Show answer and explanation

Answer: orElse

orElse() returns the value if present, otherwise returns the provided default. Safe alternative to get().

Hint: Which Optional method provides a fallback?

What this challenge tests

Code Completion in Java: optional, null-safety.

Related challenges