From e3353fd3a5cc11433488d05f10111d25fde75068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Thu, 2 Jan 2025 20:59:36 +0100 Subject: [PATCH 1/3] Explained modern switch block --- .../.docs/introduction.md | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/exercises/concept/football-match-reports/.docs/introduction.md b/exercises/concept/football-match-reports/.docs/introduction.md index ff20beb45..68d87af67 100644 --- a/exercises/concept/football-match-reports/.docs/introduction.md +++ b/exercises/concept/football-match-reports/.docs/introduction.md @@ -32,3 +32,63 @@ switch (direction) { break; } ``` + +## Modern Switch Statements + +The switch statement was improved in the latest Java versions. + +- The `break` keyword is not needed and the arrow operator is used instead of the semicolon. +- Multiple case values can be provided in a single case statement. + +```java +String direction = getDirection(); +switch (direction) { + case "left" -> goLeft(); + case "right" -> goRight(); + case "top", "bottom" -> goStraight(); + default -> markTime(); +} +``` + +The first LTS (Long Term Support) version that had these improvements was Java 17, released on September, 2021. + +Other improvement is that the case values can be any object. + +## Switch Expressions + +Going even further, the switch block is now an expression, meaning it returns a value. + +```java +return switch (day) { // switch expression + case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Week day"; + case "Saturday", "Sunday" -> "Weekend"; + default -> "Unknown"; +}; +``` + +instead of using a switch statement: + +```java +String day = ""; +switch (day) { // switch statement + case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> day = "Week day"; + case "Saturday", "Sunday" -> day = "Weekend"; + default-> day = "Unknown"; +}; +``` + +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. + From b3195d4588d41ad801b5c11a01dac4a18d4e4a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Thu, 2 Jan 2025 21:29:47 +0100 Subject: [PATCH 2/3] Delete whitespace --- exercises/concept/football-match-reports/.docs/introduction.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/concept/football-match-reports/.docs/introduction.md b/exercises/concept/football-match-reports/.docs/introduction.md index 68d87af67..235cfb29f 100644 --- a/exercises/concept/football-match-reports/.docs/introduction.md +++ b/exercises/concept/football-match-reports/.docs/introduction.md @@ -35,7 +35,7 @@ switch (direction) { ## Modern Switch Statements -The switch statement was improved in the latest Java versions. +The switch statement was improved in the latest Java versions. - The `break` keyword is not needed and the arrow operator is used instead of the semicolon. - Multiple case values can be provided in a single case statement. From a3007a57ebf3dc3a6ca091f664419f73de601570 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Ram=C3=B3n=20=28JR=29?= Date: Thu, 2 Jan 2025 21:32:01 +0100 Subject: [PATCH 3/3] Delete empty lines. --- exercises/concept/football-match-reports/.docs/introduction.md | 1 - 1 file changed, 1 deletion(-) diff --git a/exercises/concept/football-match-reports/.docs/introduction.md b/exercises/concept/football-match-reports/.docs/introduction.md index 235cfb29f..611d1a2b9 100644 --- a/exercises/concept/football-match-reports/.docs/introduction.md +++ b/exercises/concept/football-match-reports/.docs/introduction.md @@ -91,4 +91,3 @@ return switch (day) { ``` The first LTS (Long Term Support) version that had these improvements was Java 21, released on September, 2023. -