Lewati ke konten
Rama's logo Qisthi Ramadhani
Go back

Learning Note: Java 21 Feature – Pattern Matching for Switch

Continuing my journey through “Java 21 – Exploring the Latest Innovations for 2024”, I’ve reached Feature 2: Pattern Matching for Switch. This enhancement brings a major improvement to how conditional logic is handled in Java, making code both cleaner and safer.

Motivation: Cleaner and More Expressive Conditional Logic

The analogy used in the course is that of a chef preparing various ingredients—each requiring a specific tool or treatment. In traditional Java, handling different types in a switch statement was like using a single knife for all ingredients: possible, but neither efficient nor elegant. You’d often have to rely on instanceof checks and manual typecasting, which made code verbose, error-prone, and hard to read.

if (obj instanceof String) {
  String s = (String) obj;
  System.out.println(s.toLowerCase());
} else if (obj instanceof Integer) {
  Integer i = (Integer) obj;
  System.out.println(i + 1);
} else {
  System.out.println("Unknown type");
}

Pattern matching for switch addresses these pain points by allowing you to handle different data types and patterns directly in the switch statement. This results in more readable, maintainable, and expressive code.

Key Advantages

Example: Pattern Matching for Switch in Action

Here’s a simplified example inspired by the course:

public class PatternMatching {
  public static void main(String[] args) {
    System.out.println(asStringValue(1));       // int 1
    System.out.println(asStringValue("Hello")); // string Hello
    System.out.println(asStringValue(1L));      // long 1
    System.out.println(asStringValue(3.14));    // double 3.14
    System.out.println(asStringValue(true));    // unknown
  }

  static String asStringValue(Object anyValue) {
    return switch (anyValue) {
      case Integer i -> "int " + i;
      case String s -> "string " + s;
      case Long l -> "long " + l;
      case Double d -> "double " + d;
      default -> "unknown";
    };
  }
}

This code checks the type of anyValue and matches it to the appropriate case, automatically handling type casting. It’s much more concise than the old approach, which required multiple instanceof checks and manual casts.

Hands-On Takeaway

This feature is one of the highlights of Java 21, and after trying it hands-on, I can clearly see how it will simplify everyday coding tasks and make my codebase more reliable.


Share this post on:
LLM-friendly version:
Open in ChatGPT Open in Claude

Related Posts


Previous Post
Learning Note: Java 21 Feature – Unnamed Classes and Instance Main Methods (Preview)
Next Post
Learning Note: Java 21 Feature – Unnamed Patterns and Variables