Which modern Java features do you use in your own code, and why those?
answer
- a few, not a catalogue
- problem, feature, limit
- let the compiler find the cases
- scale for blocking work
- what you decline and why
basics
~20 sA strong answer picks a few and justifies each: records and sealed types with pattern switch for compiler-checked data models, virtual threads for blocking I/O at scale, text blocks for readable literals, and no preview features in production.
solid answer
~40 sI use records, sealed interfaces and pattern `switch` together for domain data: when someone adds a case, the compiler finds every `switch` without a `default` that has to change. For servers that mostly wait on I/O I use virtual threads, because plain blocking code then handles many more concurrent tasks. I do not use them for CPU-heavy work, since they are not faster. I use text blocks for multi-line literals in tests, and `var` where the type is obvious from the right-hand side. I keep preview APIs such as `StructuredTaskScope` out of production, because they need `--enable-preview` and can change between releases. The list depends on the JDK we run, so I would say which one that is.
go deeper
Pick two features you have really used, such as records and text blocks, and say what each one saved you. Being honest about what you have not used is better than listing more.
Give the mechanism behind each reason: why a switch without default is checked over a sealed type, and why virtual threads help blocking work but not CPU-heavy work.
Tie each choice to something you observed in production, such as pinning, memory or startup, and name what you declined and why, including preview APIs. The limit you hit is the strongest part of the answer.
Answer for the team as well: which features to standardize in reviews, which JDK the fleet must reach before adopting them, what the policy on preview features is, and which library dependencies decide the timing.
## What the interviewer is listening for There is no single right answer, and that is the point of the question. A list of every feature since Java 8 tells the interviewer you have read the release notes. Three features with a reason and a limit tell them you have made decisions in real code. They are listening for: - **Choice.** You name a few, not a catalogue. - **A reason tied to a problem.** "Records are nice" is not a reason. "We kept writing `equals` by hand and getting it wrong" is. - **A limit.** Knowing where a feature stops helping is what separates use from enthusiasm. - **Honesty about your JDK.** Say what your code actually runs on. Claiming virtual threads on a Java 17 service ends the conversation badly. ## A defensible set Your own list should come from your own code. This one is defensible for a backend developer on a current JDK. | Feature | Final in | Why use it | Where it stops | |---|---|---|---| | Records | 16 | A transparent carrier for data, with accessors, `equals`, `hashCode` and `toString` generated | Implicitly final, cannot extend a class, and a component that is a mutable list stays mutable | | Sealed types with pattern `switch` | 17, 21 | A closed set of cases, and a compile error when one goes unhandled | A `default` branch turns that check off. Open extension points still need ordinary interfaces | | Virtual threads | 21 | Plain blocking code handles many more concurrent tasks | No gain on CPU-heavy work. Never pool them. Native calls can still pin, and `synchronized` pins before Java 24 | | Text blocks | 15 | Readable multi-line literals, such as a JSON payload in a test | Only a literal, so it does nothing for injection safety | | `var` | 10 | Less repetition where the type is obvious | Hides the type when the right-hand side does not show it | | `java.time` | 8 | Immutable values, safe to share between threads | Old interfaces may still force conversions at the edges | ## How to build each reason Each answer should take about three sentences, in this shape: - **The problem you had.** "Adding a new payment type meant finding every `if`-chain that handled the old ones." - **The feature you chose and what it changed.** "We made the type a sealed interface and removed the `default` branches, so the next new type failed the build until each switch handled it." - **The limit you hit.** "We did not seal the plugin interface, because other teams add their own implementations of it." That small story is what the interviewer remembers, and it invites the follow-up you are ready for. ## What you decline, and why Declining a feature shows as much judgment as using one, so have an answer ready: - **Preview APIs in production.** `StructuredTaskScope` at JDK 27 needs `--enable-preview` at compile and run time, and the class files are tied to that release. Its API changed again at 27. You can try it, but shipping it means committing to rework at each upgrade. - **Incubating modules.** The Vector API is still in `jdk.incubator.vector`. Unless you have measured a need, it is not a default choice. - **Parallel streams by reflex.** Use them when you have measured a benefit. A pipeline is not faster because it says `parallelStream()`. - **Every new feature on arrival.** A new language feature that some of the team cannot read yet has a cost, which you weigh against what it saves. ## How the answer changes with seniority A developer's answer is about the code in front of them. A senior answer adds operations: what happened to memory, startup or pinning after adopting the feature, and how you checked. A lead's answer adds the team: which features to standardize in reviews, which JDK the fleet must reach first, and which libraries have to move before you can use something everywhere. If you have not used a feature, say so and say what would make you try it. An interviewer can tell the difference between use and reading, and honesty costs less than being caught out on a follow-up.
- Why leave out default in a pattern switch over a sealed type, when default looks safer?A `default` makes the switch exhaustive forever, so a new subtype compiles and silently takes that branch. Without it, javac rejects the switch until the new type is handled. That moves the error from production to the build. Keep a `default` only when unknown cases really can share one behaviour.
- When would you not use a record?When the class needs mutable state, since a record's fields are final. When it must extend another class, since a record cannot. When its internal representation should stay hidden, since a record exposes every component through an accessor. And when identity matters more than the data it holds, since its `equals` compares components.
- What would make you adopt structured concurrency before it is final?A contained case: internal tooling or one service whose JDK I control, where the error-handling benefit is large and I accept rewriting it at each upgrade, because each preview round can change the API. I would not put it in a shared library, since `--enable-preview` would spread to every consumer.
- Every new Java feature is worth adopting as soon as it ships.
- Virtual threads make any service faster, so use them everywhere.
- A preview feature is as settled as a final one, so ship it freely.
- A default branch is always the safer choice in a switch over a sealed type.
- var gives up compile-time type checking.
- A record makes everything reachable from it immutable.
- Records
- Java 16pre-release 14
- Sealed classes
- Java 17pre-release 15
- Pattern matching for switch
- Java 21pre-release 17
- Virtual threads
- Java 21pre-release 19
- Text blocks
- Java 15pre-release 13
Assumes JDK 27 as the baseline; the choice depends on the JDK your code runs on, and pinning in synchronized differs before and after Java 24.
context
- JEP 444: Virtual Threadsopenjdk.org
- JEP 441: Pattern Matching for switchopenjdk.org
- JEP 12: Preview Featuresopenjdk.org