Quick revision
The short answer
I would tell teams to use arrow-form switch expressions and pattern switches in all new code, because they remove fall-through, check exhaustiveness and return a value, but not to mass-rewrite stable code just for style. Code is rewritten when we touch it anyway, or when exhaustiveness adds real safety. Over enums and sealed types, teams should leave out default so a new constant or subtype becomes a compile error, the rules should live in the shared style guide and IDE inspections, and each module decides based on its --release target, because libraries built for older Java cannot use these features.
What to remember
In this answer, we saw that the new switch forms move forgotten breaks and forgotten cases from run time to compile time. From that we built four pieces of guidance: use them in new code, leave out default over enums and sealed types so the compiler finds every switch to update, modernise old code when touching it rather than in a mass rewrite, and decide per module by its --release target. We finished by putting the rules into the style guide and IDE inspections so reviews stay consistent.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
I would tell teams to use arrow-form switch expressions and pattern switches in all new code, because they remove fall-through, check exhaustiveness and return a value, but not to mass-rewrite stable code just for style. Code is rewritten when we touch it anyway, or when exhaustiveness adds real safety. Over enums and sealed types, teams should leave out default so a new constant or subtype becomes a compile error, the rules should live in the shared style guide and IDE inspections, and each module decides based on its --release target, because libraries built for older Java cannot use these features.
Why this question is asked
A Java upgrade is more than a new runtime. Moving from Java 11 to 21 or 25 gives teams switch expressions, pattern matching for switch, records and sealed types all at once, and without guidance every team adopts them differently: one rewrites everything in a week, another bans them, and a third uses them in a library that must still run on Java 11. Interviewers ask senior engineers this question to see how they turn a language feature into a policy that is safe, consistent and cheap. In this answer, we will recap what the new switch forms actually give us, then walk through the guidance one decision at a time, with the reasoning behind each, and finish with how to roll it out.
1A Quick Recap of What the New Forms Give Us
Java 14 made switch expressions final: arrow cases that never fall through, several labels per arm, a value returned from the switch, and yield for block arms. Java 21 made pattern matching for switch final: we can switch on the type of an object, deconstruct records in a case, add guards with when, and handle null with case null. Combined with sealed interfaces, which list every permitted subtype, the compiler can check that a switch handles every case.
So the new forms are not just shorter syntax. They move two classes of bugs, a forgotten break and a forgotten case, from run time to compile time. That safety is the argument we lead with when we write the guidance, because it is what justifies the cost of change.
2Guidance 1: New Code Uses the New Forms
For new code, the rule is simple: prefer arrow-form switch expressions when the switch computes a value, and arrow-form statements when it only performs actions. The reasoning is that new code has no migration cost, so we get the safety for free. A typical example is mapping an order status to a customer message, which used to be a switch with five breaks and a variable assigned in every branch.
Notice the second version also has no default. That is deliberate, and it is the next piece of guidance.
3Guidance 2: Leave Out default Over Enums and Sealed Types
When a switch covers every constant of an enum or every subtype of a sealed interface, a default arm is not needed, and adding one throws away the best part of the feature. Suppose next quarter someone adds REFUNDED to the status enum. With no default, every switch expression over the status stops compiling until it handles REFUNDED, so the compiler gives us a complete list of places to update. With a default, every switch silently sends refunds to the Unknown message, and we find out from customers.
If an old compiled class meets a newer enum at run time, the generated code still protects us: it throws an error instead of returning nothing (MatchException in recent versions; check your version for the exact type). The same applies to sealed hierarchies, which is where pattern switches shine.
4Guidance 3: Do Not Mass-Rewrite Stable Code
Teams will be tempted to convert every old switch in one big pull request. We advise against it. A rewrite of stable, tested code has real costs: a large diff that reviewers skim, merge conflicts with every open branch, and a small but real chance of changing behaviour, especially where old code relied on fall-through on purpose. The benefit is mostly style, because code that has worked for years is not where the missing-break bugs are hiding.
So the rule is to rewrite opportunistically: when we change a method for another reason, we modernise the switch in it, and we migrate deliberately where exhaustiveness adds safety, such as switches over an enum that is still growing. For simple mechanical conversions, IDE quick fixes or OpenRewrite recipes can convert many switches with low risk in a separate, reviewable change, but the decision to run them is per module, not organisation-wide.
5Guidance 4: Decide Per Module by Its --release Target
A service deployed on Java 21 can use everything. A shared library is different: if it still compiles with --release 11 because some consumers run on Java 11, it cannot use switch expressions or pattern switches at all, and the build will refuse them. So the guidance cannot be one rule for the whole organisation. Each module states its --release target in its build, and the target decides which forms are allowed there.
- Is the module's --release 21 or higher?YesArrow switches, expressions and pattern switchesNoCheck for 14 or higher
- Is the --release 14 or higher?YesArrow switches and expressions, no patternsNoOld colon form only
- Is this new code or code being changed anyway?YesUse the newest allowed formNoLeave it unless exhaustiveness adds safety
- Over enums and sealed typescover every case and leave out default
4 steps in order. Play them, or step through them one at a time.
The module's release target sets what is possible; whether the code is being touched sets what is worth doing.
6Rolling Out the Guidance
Rules nobody can see are not followed, and rules enforced only by reviewers are applied unevenly. So we write the four points into the shared style guide with one short example each, and we turn on matching IDE inspections and static analysis in the shared configuration, so the editor suggests the arrow form and flags a default over a fully covered enum. In reviews, the question becomes whether the code follows the guide, not a matter of taste. After a quarter, we look at what reviewers still comment on and adjust the guide, rather than adding more rules up front.
7Follow-up questions
Should teams also adopt records and sealed types during the upgrade?
Yes, where they model the domain better, and they are what make pattern switches most useful. But the same rule applies: use them in new code and in code being changed, and do not rewrite stable classes only to use records, especially if they are mapped by frameworks that may need adjusting.
What about preview features in Java 25?
Preview features need --enable-preview to compile and run, and they can change between releases. I would allow them only in experiments and test builds, never in production code, and wait until they are final in an LTS release before adding them to the style guide.
How do you handle intentional fall-through in old code that you are not rewriting?
We leave it, and add a comment saying the fall-through is deliberate, so readers and checkers such as Checkstyle's fall-through rule accept it. When that code is next changed, we usually convert it to an arrow case with several labels.
8Conclusion
In this answer, we saw that the new switch forms move forgotten breaks and forgotten cases from run time to compile time. From that we built four pieces of guidance: use them in new code, leave out default over enums and sealed types so the compiler finds every switch to update, modernise old code when touching it rather than in a mass rewrite, and decide per module by its --release target. We finished by putting the rules into the style guide and IDE inspections so reviews stay consistent.
Open with
My guidance would have four parts. First, new code uses arrow-form switch expressions when it computes a value, and pattern switches where they fit, because they remove fall-through, return a value and are checked for exhaustiveness, so two kinds of bugs become compile errors.
Then, if they want more
Second, over enums and sealed types, cover every case and leave out default. That way, when someone adds a new status or a new permitted subtype, every switch fails to compile until it is handled, instead of a default silently treating it as unknown. Third, I would not mass-rewrite stable code for style.
A big conversion PR costs review attention, causes merge conflicts and can change behaviour where old code fell through on purpose. We modernise a switch when we touch that code anyway, or deliberately where exhaustiveness adds safety, and we can use IDE quick fixes or OpenRewrite for simple cases in separate changes.
Fourth, it is decided per module: a library still compiled with release 11 for older consumers cannot use these features, while a service on 21 or 25 can use all of them. Finally, I would write this into the shared style guide and IDE inspections so reviews are consistent.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11, Java 21, Java 14
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Your organization is moving from Java 11 to Java 21 or 25. What guidance would you give...": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/25135acfa7/