Skip to content
Learn Netverks

Lesson

Step 30/36 83% through track

optional-records

Optional and records

Last reviewed May 28, 2026 Content v20260528
Track mode
server_compiled
Means
Compiled runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Optional and records: the syntax, APIs, and habits you need before advancing in Java.

Teams ship Optional and records on every Java codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Optional and records in contexts like: Spring Boot APIs, banking systems, Android (with Kotlin), and batch/data pipelines on the JVM.

Write Java with a public class (lessons use Main), click Run on server—the dev runner runs javac then java; fix compile errors from stderr (LEARNING_RUNNER_ENABLED=true).

Toward the end of the track—consolidate before capstone-style review lessons.

Optional<T> models absent values without null—use for return types, not fields or parameters. Records (Java 16+) are immutable data carriers with generated equals/hashCode/toString.

Optional

Optional<User> find(String id) {
    return Optional.ofNullable(cache.get(id));
}

find("1").map(User::name).orElse("Guest");

Record

record User(String email, String name) {}

Important interview questions and answers

  1. Q: Optional vs null?
    A: Optional forces callers to consider absence; avoid Optional.get() without isPresent—use orElse/orElseThrow.
  2. Q: Record vs class?
    A: Record for immutable data; class when behavior and mutable state dominate.

Self-check

  1. What method safely returns a default from Optional?
  2. Are record fields mutable by default?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Optional.get() danger?
  • Record vs class DTO?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump