Language syntax
Java switch expression
Produce a value with exhaustive selection. Stable since Java 14; include default for ordinary int labels that do not cover every value.
Formal Syntax & Structure
java syntaxjava
int result = switch (selector) { case 1 -> 10; default -> 0; };Working Example
Example Snippetjava
public class Main { public static void main(String[] args) { int code = 1; int result = switch (code) { case 1 -> 10; default -> 0; }; System.out.println(result); }}