Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions concepts/switch-statement/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,23 @@ Starting with Java 14 (available as a preview before in Java 12 and 13) it is po
However if you use the new `->` notation it must be followed by either: a single statement/expression, a `throw` statement or a `{}` block.
No more confusion!

The first LTS (Long Term Support) version that had these improvements was Java 17, released on September, 2021.

In addition, a feature called `Guarded Patterns` was added, which allows you to do checks in the case label itself.

```java
String dayOfMonth = getDayOfMonth();
String day = "";
return switch (day) {
case "Tuesday" && dayOfMonth == 13 -> "Forbidden day!!";
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Week day";
case "Saturday", "Sunday" -> "Weekend";
default -> "Unknown";
};
```

The first LTS (Long Term Support) version that had these improvements was Java 21, released on September, 2023.

You can find more information on enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc].

[yield-keyword]: https://www.codejava.net/java-core/the-java-language/yield-keyword-in-java
Expand Down