Blog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2025 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Java
Javamedium

What is `Optional` used for, and what is a common misuse?

Tags
#optional#null-safety#api
Back to categoryPractice quiz

Answer

`Optional` represents a value that may be present or absent and forces the caller to handle the empty case. A common misuse is using it as a field in entities/DTOs everywhere; it’s mainly intended for return values, not for serialization or persistence fields.

Optional<User> user = repo.findById(id);
String name = user.map(User::getName).orElse("unknown");

Related questions

Next.js
Route Handlers in the App Router: how do you define them and what are they used for?
#nextjs#route-handlers#api
Kotlin
`lateinit` vs nullable property: when would you choose `lateinit`?
#kotlin#lateinit#null-safety
Microservices
BFF (Backend for Frontend): what is it and when does it help?
#microservices#bff#api
Kotlin
Kotlin smart casts: when do they work and when do they not?
#kotlin#smart-cast#type-safety
Microservices
If services can’t share a DB, how do they share data?
#contracts#events#api
Next.js
What is a Route Handler (`route.ts`) and when would you use it?
#route-handlers#api#app-router