In the previous tutorial, I introduced you to the basic knowledge of the switch statement in Java. In this tutorial, I will talk more about the new features that Java supports from version 14 onwards!
Switch from Java 14
When working with a switch statement, you can encounter the following problems:
- The first problem is that we forget to declare the break statement: this means that our code will not exit the switch statement after matching the first case but will continue the next case, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int a = 1; int b = 2; switch (a + b) { case 2: System.out.println("Khanh"); break; case 3: System.out.println("Huong Dan Java"); case 4: System.out.println("Lap Trinh Java"); default: System.out.println("Hello"); break; } } } |
Result:
- The second problem is if we want in case the value of the variable with different values a and b, we will handle the same code. We will write the following code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int a = 1; int b = 2; switch (a + b) { case 2: System.out.println("Khanh"); break; case 3: System.out.println("Huong Dan Java"); break; case 4: System.out.println("Huong Dan Java"); break; default: System.out.println("Hello"); break; } } } |
Or:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int a = 1; int b = 2; switch (a + b) { case 2: System.out.println("Khanh"); break; case 3: case 4: System.out.println("Huong Dan Java"); break; default: System.out.println("Hello"); break; } } } |
These ways of writing are inconvenient because of duplicating the code, or we sometimes can’t control it, where should we put a break statement?
- The third problem is that if you want to use a switch statement to determine the value of a variable, you must write the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { String message = ""; int n = 1; switch (n) { case 1: message = "Khanh"; break; case 2: message = "Huong Dan Java"; break; case 3: message = "Lap Trinh Java"; break; default: break; } System.err.println(message); } } |
To solve these problems, from Java 14, you can write a switch statement with the following improvements:
- For the first problem, Java 14 now supports us in writing a switch statement like a lambda expression, for example. as:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int a = 1; int b = 2; switch (a + b) { case 2 -> System.out.println("Khanh"); case 3 -> System.out.println("Huong Dan Java"); case 4 -> System.out.println("Lap Trinh Java"); default -> System.out.println("Hello"); } } } |
Result:
- For the second problem, you can rewrite the switch statement as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int a = 1; int b = 2; switch (a + b) { case 2 -> System.out.println("Khanh"); case 3, 4 -> System.out.println("Huong Dan Java"); default -> System.out.println("Hello"); } } } |
Result:
- As for the third problem, now Java 12 has supported us to return a value with a switch expression as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int n = 1; var message = switch (n) { case 1 -> "Khanh"; case 2 -> "Huong Dan Java"; case 3 -> "Lap Trinh Java"; default -> "Hello"; }; System.out.println(message); } } |
Result:
If you want to add code to handle business logic in each case of the switch expression, then you can add the code to use the yield keyword to return the value you want. The example is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int n = 1; var message = switch (n) { case 1 -> { String firstName = "Khanh"; String lastName = "Nguyen"; String fullName = firstName + " " + lastName; yield fullName; } case 2 -> "Huong Dan Java"; case 3 -> "Lap Trinh Java"; default -> "Hello"; }; System.out.println(message); } } |
Result:
Switch from Java 16
Since Java 16, the switch supports pattern matching similar to the instanceof operator from Java 14.
For example, I have an interface with two implementations as follows:
1 2 3 4 5 |
package com.huongdanjava.javaexample; public interface Shape { } |
1 2 3 4 5 |
package com.huongdanjava.javaexample; public class Triangle implements Shape { } |
1 2 3 4 5 |
package com.huongdanjava.javaexample; public class Rectangle { } |
To check and print the implementation of a Shape object, we would normally write code like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.javaexample; public class SwitchExample { public static void main(String[] args) { Shape shape = new Triangle(); if (shape instanceof Triangle triangle) { System.out.println("This is triangle!"); } else if (shape instanceof Rectangle rectangle) { System.out.println("This is rectangle!"); } else { throw new IllegalArgumentException("Unrecognized shape!"); } } } |
From Java 16, you can write briefly with the switch as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.huongdanjava.javaexample; public class SwitchExample { public static void main(String[] args) { Shape shape = new Triangle(); switch (shape) { case Triangle triangle -> System.out.println("This is triangle!"); case Rectangle rectangle -> System.out.println("This is rectangle!"); default -> throw new IllegalArgumentException("Unrecognized shape!"); } } } |
In this case, you must declare the default case! Because the shape object may not be in the implementations that we declared in the cases.
Switch từ Java 23 Preview
Since Java 23 Preview, switch statement supports pattern matching with primitive types! That means, now you can write:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.java; public class SwitchExample { public static void main() { test(1); } static void test(Object obj) { if (obj instanceof int i) { System.out.println("It's an int: " + i); } } } |
Result: