Skip to content

Commit 2bc849f

Browse files
authored
Correct info about the Guarded Patterns
1 parent c9cd6a9 commit 2bc849f

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

concepts/switch-statement/about.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,24 +145,25 @@ Starting with Java 14 (available as a preview before in Java 12 and 13) it is po
145145
However if you use the new `->` notation it must be followed by either: a single statement/expression, a `throw` statement or a `{}` block.
146146
No more confusion!
147147

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

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

151152
```java
152153
String dayOfMonth = getDayOfMonth();
153154
String day = "";
154155
return switch (day) {
155-
case "Tuesday" && dayOfMonth == 13 -> "Forbidden day!!";
156+
case "Tuesday" when dayOfMonth == 13 -> "Forbidden day!!";
156157
case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" -> "Week day";
157158
case "Saturday", "Sunday" -> "Weekend";
158159
default -> "Unknown";
159160
};
160161
```
161162

162-
163-
You can find more information on enhanced switch [here][switch1], [here][switch2] and on the [oracle documentation][oracle-doc].
163+
You can find more information on the switch expression on Java 21 [here][switch-on-Java-21]
164164

165165
[yield-keyword]: https://www.codejava.net/java-core/the-java-language/yield-keyword-in-java
166166
[switch1]: https://www.vojtechruzicka.com/java-enhanced-switch/
167167
[switch2]: https://howtodoinjava.com/java14/switch-expressions/
168168
[oracle-doc]: https://docs.oracle.com/en/java/javase/13/language/switch-expressions.html
169+
[switch-on-Java-21]: https://blog.adamgamboa.dev/switch-expression-on-java-21/#3-guarded-pattern

0 commit comments

Comments
 (0)