Skip to content

Commit 28c8836

Browse files
author
Nick
committed
Code samples for Java Switch Statement
1 parent 0fd3f51 commit 28c8836

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>core-java-13</artifactId>
7+
<version>0.1.0-SNAPSHOT</version>
8+
<name>core-java-13</name>
9+
<packaging>jar</packaging>
10+
<url>http://maven.apache.org</url>
11+
12+
<parent>
13+
<groupId>com.baeldung</groupId>
14+
<artifactId>parent-modules</artifactId>
15+
<version>1.0.0-SNAPSHOT</version>
16+
<relativePath>../../</relativePath>
17+
</parent>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.assertj</groupId>
22+
<artifactId>assertj-core</artifactId>
23+
<version>${assertj.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-compiler-plugin</artifactId>
33+
<version>${maven-compiler-plugin.version}</version>
34+
<configuration>
35+
<source>${maven.compiler.source.version}</source>
36+
<target>${maven.compiler.target.version}</target>
37+
<release>13</release>
38+
<compilerArgs>--enable-preview</compilerArgs>
39+
</configuration>
40+
</plugin>
41+
<plugin>
42+
<groupId>org.apache.maven.plugins</groupId>
43+
<artifactId>maven-surefire-plugin</artifactId>
44+
<version>3.0.0-M3</version>
45+
<configuration>
46+
<argLine>--enable-preview</argLine>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
<properties>
53+
<maven.compiler.source.version>13</maven.compiler.source.version>
54+
<maven.compiler.target.version>13</maven.compiler.target.version>
55+
<assertj.version>3.6.1</assertj.version>
56+
</properties>
57+
58+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.baeldung.switchExpression;
2+
3+
import static java.time.Month.AUGUST;
4+
import static java.time.Month.JUNE;
5+
6+
import static org.junit.Assert.assertEquals;
7+
8+
import java.time.Month;
9+
import java.util.function.Function;
10+
11+
import org.junit.Test;
12+
13+
public class SwitchExpressionsUnitTest {
14+
15+
@Test
16+
@SuppressWarnings ("preview")
17+
public void switchExpression() {
18+
19+
var month = JUNE;
20+
21+
var result = switch (month) {
22+
case JANUARY, JUNE, JULY -> 3;
23+
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
24+
case MARCH, MAY, APRIL -> 2;
25+
default -> 0;
26+
};
27+
28+
assertEquals(result, 3);
29+
}
30+
31+
@Test
32+
@SuppressWarnings ("preview")
33+
public void switchExpressionWithYieldKeyword() {
34+
var month = AUGUST;
35+
36+
var result = switch (month) {
37+
case JANUARY, JUNE, JULY -> 3;
38+
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
39+
case MARCH, MAY, APRIL, AUGUST -> {
40+
int monthLength = month.toString().length();
41+
yield monthLength * 4;
42+
}
43+
default -> 0;
44+
};
45+
46+
assertEquals(24, result);
47+
}
48+
49+
@Test
50+
@SuppressWarnings ("preview")
51+
public void switchStatementWithReturnInsideBlock() {
52+
53+
Function<Month, Integer> func = (month) -> {
54+
switch (month) {
55+
case JANUARY, JUNE, JULY -> { return 3; }
56+
default -> { return 0; }
57+
}
58+
};
59+
60+
assertEquals(Integer.valueOf(3), func.apply(Month.JANUARY));
61+
}
62+
63+
@Test
64+
@SuppressWarnings ("preview")
65+
public void switchExpressionWithAllCasesCovered() {
66+
var month = AUGUST;
67+
68+
var result = switch (month) {
69+
case JANUARY, JUNE, JULY -> 3;
70+
case FEBRUARY, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 1;
71+
case MARCH, MAY, APRIL, AUGUST -> 2;
72+
};
73+
74+
assertEquals(result, 2);
75+
}
76+
}

0 commit comments

Comments
 (0)