Java 17 and 21 Switch statement and expression summary

Switch Statement (w/o pattern matching) Summary


Old Syntax ( : ) New Syntax( -> )
  Allows zero or more individual statements or zero or more code blocks.
Examples (all valid):
case 0:
case 1: stmt1; stmt2;
case 2: { <code block1>} { <code block 2> }
Allows either exactly one statement or exactly one code block.
Examples (all invalid):
case 0 -> //invalid, empty case
case 2 -> stmt1; stmt2; //invalid, more than one statement
case 4 -> { } { }//invalid, more than one block

Selector expression type Limited to: byte, short, int, char, and long and their wrappers, String, and enums
Not Allowed: boolean, float, and double
Limited to: byte, short, int, char, and long and their wrappers, String, and enums
Not Allowed: boolean, float, and double
Case label constant type Must be compatible with selector expression type. null is allowed in Java 21 but only if the switch is exhaustive. Must be compatible with selector expression type. null is allowed in Java 21 but only if the switch is exhaustive.
case label

Allows a single case constant. Allows multiple case constants separated by comma. Example:
case 1, 2 -> { }
default label  Not required Not required
Order of switch labels No restriction No restriction
 break Needed only to prevent fall through
Prohibited when a switch label is associated with a single statement.

Allowed within the code block  associated with a switch label but is redundant.
 yield Not allowed
Not allowed
 exhaustive Not neccessary  Not neccessary 



Switch Expression (w/o pattern matching) Summary


Old Syntax ( : ) New Syntax( -> )
  Allows one expression or one code block.
Examples:
case 0: yield 1; //valid, returns 1
case 3: yield "aa".length(); //valid, returns 2. case 2: "a".length(); //syntactically valid, no yield required for non-void method call but falls through without returning a value

case 1: 1; //invalid, missing yield keyword
Allows one expression or one code block.
Examples:
case 1 -> 1; //valid, returns 1, no yield required
case 3 -> "a".length();//valid, returns 1, no yield required
case 4 -> { yield 1; }//valid, yield is required in a block

case 0 -> //invalid, missing expression
case 2 -> yield 1; //invalid, must not write yield
Selector expression type Limited to: byte, short, int, char, and long and their wrappers, String, and enums
Not Allowed: boolean, float, and double
Limited to: byte, short, int, char, and long and their wrappers, String, and enums
Not Allowed: boolean, float, and double
Case label constant type Must be compatible with selector expression type. null is allowed in Java 21 but only if the switch is exhaustive. Must be compatible with selector expression type. null is allowed in Java 21 but only if the switch is exhaustive.
case label

Allows a single case constant. Allows multiple case constants separated by comma.
Example: case 1, 2 -> 1;
default label  Not required if already exhaustive Not required if already exhaustive
Order of switch labels No restriction No restriction
 break Prohibited Prohibited
 yield Required to return value
Required only when returning a value from a code block. Otherwise prohibited.
 exhaustive Neccessary  Neccessary 


Switch Statements and Expressions (w pattern matching) Summary (Java 21 Only)


Old Syntax ( : ) New Syntax( -> )
Selector expression type All types are allowed All types are allowed
Type of case constants Must be compatible with the type of the selector expression Must be compatible with the type of the selector expression
 exhaustive Necessary Necessary
default label 

Not required if already exhaustive
Not required if already exhaustive
Order of switch labels
Must follow the rule of dominance Must follow the rule of dominance
Fall through behavior Not allowed. So, break is required after each switch label. Fall through is not possible. break is prohibited.