Class SwitchStmt

All Implemented Interfaces:
NodeWithRange<Node>, NodeWithTokenRange<Node>, SwitchNode, Observable, Visitable, HasParentNode<Node>, Cloneable

public class SwitchStmt extends Statement implements SwitchNode

The switch statement

Java 1.0-1.4

The basic C-like switch statement. It can switch only on integers.
switch(x) { case 5: case 6: a=100; break; case 9: a=33; break; default: throw new IllegalStateException(); };
In switch(a) { ... } the selector is "a", and the contents of the { ... } are the entries.

Java 5-6

Switching can now also be done on enum constants.

Java 7-11

Switching can now also be done on strings.

Java 12

In preparation for pattern matching, lots of changes are made:
  • multiple labels per case
  • a -> syntax that does not fall through.
  • break can take any expression (usable in the SwitchExpr)
  • switch can be used as an expression (it becomes a SwitchExpr)
switch(x) { case BANANA,PEAR: b=10; break; default: b=5; };
switch(x) { case 5,6 -> println("uhuh"); default -> println("nope"); };

Java 13

The break statement has been reverted to what it was before Java 12, and break-with-value is now the YieldStatement.
Author:
Julio Vilmar Gesser
See Also:
SwitchEntry, SwitchExpr, SwitchNode, BreakStmt, YieldStmt