From ea2b9157631dde1bb2a8d69740d89662b04bc767 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Fri, 3 Jan 2025 17:31:55 +0100 Subject: [PATCH 1/7] Small improvement about the enhanced switch block Add example with guarded patterns and which LTS Java versions have these features. --- concepts/switch-statement/about.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 14e0f8432..c3e8dda15 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -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 From f08d61401785e4a9e03a34d6e53e9bde9211edf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Sat, 4 Jan 2025 00:31:30 +0100 Subject: [PATCH 2/7] Update concepts/switch-statement/about.md Co-authored-by: Kah Goh --- concepts/switch-statement/about.md | 1 - 1 file changed, 1 deletion(-) diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index c3e8dda15..73cee56c4 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -145,7 +145,6 @@ 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. From 6a8fb92bde47522947563fd0b4b2fe72bddc2bfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Sat, 4 Jan 2025 00:32:12 +0100 Subject: [PATCH 3/7] Update concepts/switch-statement/about.md Co-authored-by: Kah Goh --- concepts/switch-statement/about.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 73cee56c4..3ed088359 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -146,7 +146,7 @@ Starting with Java 14 (available as a preview before in Java 12 and 13) it is po No more confusion! -In addition, a feature called `Guarded Patterns` was added, which allows you to do checks in the case label itself. +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(); From c9cd6a9de623b147b9a0eed1375a235ff37e2d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Sat, 4 Jan 2025 00:33:06 +0100 Subject: [PATCH 4/7] Update concepts/switch-statement/about.md Co-authored-by: Kah Goh --- concepts/switch-statement/about.md | 1 - 1 file changed, 1 deletion(-) diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 3ed088359..8a54953ab 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -159,7 +159,6 @@ return switch (day) { }; ``` -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]. From 2bc849f258a18aa34e0170ab4870e6e52c338e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Sat, 4 Jan 2025 00:46:03 +0100 Subject: [PATCH 5/7] Correct info about the Guarded Patterns --- concepts/switch-statement/about.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/concepts/switch-statement/about.md b/concepts/switch-statement/about.md index 8a54953ab..9d53f54cb 100644 --- a/concepts/switch-statement/about.md +++ b/concepts/switch-statement/about.md @@ -145,6 +145,7 @@ 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! +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. @@ -152,17 +153,17 @@ In addition, a feature called `Guarded Patterns` was added in Java 21, which all String dayOfMonth = getDayOfMonth(); String day = ""; return switch (day) { - case "Tuesday" && dayOfMonth == 13 -> "Forbidden 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 enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc]. +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 From 056870f49d7b9dbe9661c3794c9f3f37cae011a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Mon, 6 Jan 2025 14:14:55 +0100 Subject: [PATCH 6/7] Update links.json --- concepts/switch-statement/links.json | 4 ++++ 1 file changed, 4 insertions(+) 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" } ] From 619b89bad1f6a32f5c5f55a5a95feac3506cf1b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Mon, 6 Jan 2025 14:16:07 +0100 Subject: [PATCH 7/7] Update contributors field. --- concepts/switch-statement/.meta/config.json | 1 + 1 file changed, 1 insertion(+) 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" ] }