diff --git a/concepts/switch-statement/.meta/config.json b/concepts/switch-statement/.meta/config.json index ce9f08c55..62882afb5 100644 --- a/concepts/switch-statement/.meta/config.json +++ b/concepts/switch-statement/.meta/config.json @@ -4,5 +4,6 @@ "Azumix" ], "contributors": [ + "josealonso" ] } diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 14e0f8432..9d53f54cb 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -147,7 +147,23 @@ Starting with Java 14 (available as a preview before in Java 12 and 13) it is po You can find more information on enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc]. +In addition, a feature called `Guarded Patterns` was added in Java 21, which allows you to do checks in the case label itself. + +```java +String dayOfMonth = getDayOfMonth(); +String day = ""; +return switch (day) { + case "Tuesday" when dayOfMonth == 13 -> "Forbidden day!!"; + case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Week day"; + case "Saturday", "Sunday" -> "Weekend"; + default -> "Unknown"; +}; +``` + +You can find more information on the switch expression on Java 21 [here][switch-on-Java-21] + [yield-keyword]: https://www.codejava.net/java-core/the-java-language/yield-keyword-in-java [switch1]: https://www.vojtechruzicka.com/java-enhanced-switch/ [switch2]: https://howtodoinjava.com/java14/switch-expressions/ [oracle-doc]: https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html +[switch-on-Java-21]: https://blog.adamgamboa.dev/switch-expression-on-java-21/#3-guarded-pattern diff --git a/concepts/switch-statement/links.json b/concepts/switch-statement/links.json index 11502d493..c428d3386 100644 --- a/concepts/switch-statement/links.json +++ b/concepts/switch-statement/links.json @@ -14,5 +14,9 @@ { "url": "https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html", "description": "oracle-doc" + }, + { + "url": "https://blog.adamgamboa.dev/switch-expression-on-java-21/#3-guarded-pattern", + "description": "switch-on-Java-21" } ]