GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Pattern matching in switch | java.evolved

Home / Language / Pattern matching in switch
Language Intermediate

Pattern matching in switch

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Replace if-else instanceof chains with clean switch type patterns.

Pattern Matching Switch Expressions
Code Comparison
✕ Java 8
String format(Object obj) {
    if (obj instanceof Integer i)
        return "int: " + i;
    else if (obj instanceof Double d)
        return "double: " + d;
    else if (obj instanceof String s)
        return "str: " + s;
    return "unknown";
}
✓ Java 21+
String format(Object obj) {
    return switch (obj) {
        case Integer i -> "int: " + i;
        case Double d  -> "double: " + d;
        case String s  -> "str: " + s;
        default        -> "unknown";
    };
}
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
📐

Structured dispatch

Switch makes the branching structure explicit and scannable.

🎯

Expression form

Returns a value directly — no mutable variable needed.

✅

Exhaustiveness

The compiler ensures all types are handled.

Old Approach
if-else Chain
Modern Approach
Type Patterns
Since JDK
21
Difficulty
Intermediate
JDK Support
Pattern matching in switch
Available

Widely available since JDK 21 LTS (Sept 2023)

How it works

Pattern matching in switch lets you match on types directly, combining the type test, cast, and binding in one concise case label. The compiler checks completeness.

Related Documentation
Pattern Matching for switch (JEP 441) ↗
Proof
View proof source ↗
Related patterns
Language Beginner

Pattern matching for instanceof

Java 8
if (obj instanceof String) {
    String s = (String) obj;
    int length = s.length();
    // do something with 'length'
}
Java 16+
if (obj instanceof String s) {
   int length = s.length();
    // do something with 'length'
}
Hover to see modern ➜
JDK 16+ →
Language Beginner

Unnamed variables with _

Java 8
try {
    parse(input);
} catch (Exception ignored) {
    log("parse failed");
}
map.forEach((key, value) -> {
    process(value); // key unused
});
Java 22+
try {
    parse(input);
} catch (Exception _) {
    log("parse failed");
}
map.forEach((_, value) -> {
    process(value);
});
Hover to see modern ➜
JDK 22+ →
Language Beginner

Records for data classes

Java 8
public class Point {
    private final int x, y;
    public Point(int x, int y) { ... }
    public int getX() { return x; }
    public int getY() { return y; }
    // equals, hashCode, toString
}
Java 16+
public record Point(int x, int y) {}
Hover to see modern ➜
JDK 16+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.