BackendBench
Coffee

Unravel โ€บ Java concepts โ€บ Language basics

Java language

Switch expressions and pattern matching

Modern switch can be an expression that produces a value, with arrow cases that do not fall through.

๐Ÿ”Ž Unravel this example

At a glance

Mental model

A sorting machine that routes each item to exactly one chute and hands back a label.

What happens, in order
  1. 1Arrow form: case 1 -> "one"; runs only that caseSeveral labels: case 1, 2 -> .... As an expression: String s = switch (x) { case 1 -> "a"; default -> "b"; };.
  2. 2In the old colon form, execution falls through to the next case unless you write break, a classic source of bugs
  3. 3if (obj instanceof String s && s.length() > 3) checks the type and binds s without a castNewer versions also allow type patterns in switch.
Remember
Watch out
Forgetting break in colon-style switchesUsing default and so hiding missing cases for enums
Example (highlighted lines explain it)
String label = switch (status) {    case NEW, PENDING -> "waiting";    case PAID -> "done";    default -> "unknown";};if (shape instanceof Circle c && c.radius() > 10) {    log.info("big circle {}", c.radius());}
Read the full explanation (about 1 min)

Modern switch can be an expression that produces a value, with arrow cases that do not fall through. instanceof patterns test a type and name the result in one step.

A mental model

A sorting machine that routes each item to exactly one chute and hands back a label. The old switch dropped items down every chute below the match unless you stopped it with break.

How it works

Arrow form: case 1 -> "one"; runs only that case. Several labels: case 1, 2 -> .... As an expression: String s = switch (x) { case 1 -> "a"; default -> "b"; };.

In the old colon form, execution falls through to the next case unless you write break, a classic source of bugs.

if (obj instanceof String s && s.length() > 3) checks the type and binds s without a cast. Newer versions also allow type patterns in switch.

Going deeper

A switch expression must be exhaustive: cover every case or add default. With enums and sealed types the compiler can check this, so adding a new constant makes forgotten switches fail to compile, which is a feature.

Under the hood

String switches compile to a hash-based lookup then a comparison; enum switches use a lookup table over ordinals; pattern switches use invokedynamic (SwitchBootstraps).

Common mistakes

  • Forgetting break in colon-style switches.
  • Using default and so hiding missing cases for enums.

How Unravel recognises it

When you paste code, Unravel looks for these signs of switch expressions and pattern matching. Spotting them yourself is a good habit when you read unfamiliar code:

Part of a learning path

Related concepts

Frequently asked questions

What is Switch expressions and pattern matching?

Modern switch can be an expression that produces a value, with arrow cases that do not fall through. instanceof patterns test a type and name the result in one step.

What are the most common mistakes with Switch expressions and pattern matching?

Forgetting break in colon-style switches. Using default and so hiding missing cases for enums.

How can I see Switch expressions and pattern matching in real code?

Paste your own Java into Unravel and it will point out where switch expressions and pattern matching appears, explain how it works at the detail level you choose, and draw the flow of the code around it.