-
-
Notifications
You must be signed in to change notification settings - Fork 690
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc69bf0
commit e3353fd
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,63 @@ switch (direction) { | |
break; | ||
} | ||
``` | ||
|
||
## Modern Switch Statements | ||
|
||
The switch statement was improved in the latest Java versions. | ||
Check failure on line 38 in exercises/concept/football-match-reports/.docs/introduction.md
|
||
|
||
- 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. | ||
|