Pattern matching for switch


Source code

1class PatternMatchingForSwitch {
2
3 int methodForNotNull(Object object) {
4 return switch (object) {
5 case String s -> s.length();
6 case Integer i -> i.intValue();
7 default -> 0;
8 };
9 }
10
11 int methodForNullable(Object object) {
12 return switch (object) {
13 case null -> 100;
14 case Integer i -> i.intValue();
15 case String s -> s.length();
16 default -> 0;
17 };
18 }
19
20}

Bytecode



Java compiler version: 21
Other examples
Main page