Java 8 Optional Interview Questions & Answers
This guide explains the purpose of the Optional class in Java 8 and how it is effectively used in real-world database operations.
1. Why was the Optional class introduced?
Optional is a container object used to represent the presence or absence of a value. It was introduced to:
- Avoid NullPointerExceptions: Provide a safer alternative to returning
null. - Clean Code: Reduce the need for explicit and repetitive null checks (
if (obj != null)). - Expressive API: Explicitly tell the consumer of a method that a result might be missing.
2. Why is the get() method considered flawed?
The get() method is planned for deprecation in future Java versions because it throws a NoSuchElementException if the value is not present. This defeats the purpose of Optional, which is to avoid unforeseen runtime exceptions.
- Recommendation: Instead of
get(), use safer alternatives likeisPresent(),orElse(), orifPresent().
3. How to use Optional with findById()?
In modern Spring Data JPA, findById() returns an Optional<T>. Here is the correct way to handle the result:
Optional<Employee> empOpt = repository.findById(id);
if (empOpt.isPresent()) {
Employee emp = empOpt.get(); // Safe to use get() here
return ResponseEntity.ok(emp);
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
Alternatively, as of Java 11, you can use isEmpty() for the opposite check.
4. Different ways to create an Optional object
Optional.of(value): Use this only if you are 100% sure the value is not null. It throws aNullPointerExceptionif the value is null.Optional.ofNullable(value): The safest way to create an Optional. It returns an empty Optional if the value is null.Optional.empty(): Returns an empty Optional instance.
5. Functional Checks: ifPresent() and ifPresentOrElse()
ifPresent(Consumer): Executes the given action only if a value is present.nameOpt.ifPresent(name -> System.out.println("Name is: " + name));ifPresentOrElse(Consumer, Runnable)(Java 9+): Executes the first action if a value is present, otherwise executes the second (empty) action.
6. Coding Challenge: Handle Name in Upper Case
Question: Fetch an employee by ID and print their name in upper case. If the name is null, print "Name is Null".
Optional<String> nameOpt = Optional.ofNullable(emp.getName());
nameOpt.ifPresentOrElse(
name -> System.out.println(name.toUpperCase()),
() -> System.out.println("Name is Null")
);