On this page
  1. switch expressions
  2. Availability
  3. Syntax & Overview
  4. Notes & Pitfalls
  5. Example

switch expressions

Prev: `primitives` | Up: builtins | Next: `Object wait notify`
Rel: builtins, Classes, Records & Sealed Hierarchies
Ref: Java Docs — switch expressions
Tags: #public #prog #java #builtins #tier/core


Availability

Built-in / java.lang (Language Expression (Java 14+/21+)) — available with zero import statements.

Syntax & Overview

var label = switch (shape) { case Circle(var r) -> ...; };

Expression-valued switch with -> arrows (no fall-through bugs), pattern matching, when guards, and compile-time exhaustiveness on enum/sealed types.

Notes & Pitfalls

Example

String day = "SAT";
boolean weekend = switch (day) {
    case "SAT", "SUN" -> true;
    default -> false;
};
assert weekend;

Linked from

Related notes