Switch statement in Java is a statement used to check the value of a variable, which can have different values, depending on the value of this variable we can define a different set of statements.
For example, I have n variable that can be valued at 1,2,3. I want that in case the value of this n variable is 1, I will print the word “Khanh”, the value of 2 will print the word “Huong Dan Java”, the value of 3 will print “Lap Trinh Java “, using a switch statement, I 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 |
package com.huongdanjava; public class SwitchExample { public static void main(String[] args) { int n = 1; switch (n) { case 1: System.out.println("Khanh"); break; case 2: System.out.println("Huong Dan Java"); break; case 3: System.out.println("Lap Trinh Java"); break; default: break; } } } |
Here, we use the case label to handle each different value of n variable. The declared value for each case must be COMPILE TIME CONSTANTS. This means that when compiling source code, the value of each case must be determined, not waiting until the program runs, execute to this switch code, the value of each case will then be determined.
To execute only the code for each value of n variable, we must use the break keyword so that after executing the code, our program will exit the switch statement.
As you can see, we also have a default label, this label is used to handle cases where the value of this n variable is not in the case where the switch statement has been processed. In a switch statement, we may not need to declare the default label, if it is declared then it is not necessary to declare on a certain order!
The result when executing the above code will look like this:
As you can see, because the value of n variable, in this case, is 1, the case for value 1 will be executed and print “Khanh”. After printing the text, to the break statement, our program will exit the switch statement.
Before Java 7, you can pass the variable of type char, byte, short, int or their wrapper Character, Byte, Short, Integer and Enum in a switch statement. From Java 7 onwards, the switch statement also supports String data type.
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) { String name = "B"; switch (name) { case "A": System.out.println("Khanh"); break; case "B": System.out.println("Huong Dan Java"); break; case "C": System.out.println("Lap Trinh Java"); break; default: break; } } } |
Result:
You can pass into the switch statement an expression as long as the return value of this expression is the allowable type of the switch statement.
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 a = 1; int b = 3; switch (a + b) { case 2: System.out.println("Khanh"); break; case 3: System.out.println("Huong Dan Java"); break; case 4: System.out.println("Lap Trinh Java"); break; default: break; } } } |
Result:
Switch from Java 12
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 12, you can write a switch statement with the following improvements:
- For the first problem, Java 12 now supports us on how to write a switch statement like 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 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:
Switch from Java 13
If you want to add code to handle business logic in each case of the switch expression, then you can upgrade to Java 13 and 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 17
Since Java 17, 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 17, 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.