Switch
case in java is alternative to if else if ladder. It is used to execute statements based on some conditions.
Table of Contents
Syntax of Switch case in java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
switch(expression) { case value_1 : // Statements break; // optional case value_2 : // Statements break; // optional // Default is executed when the expression does not match with any of the above conditions. default : // Optional // Statements } } |
Let’s understand it with the help of simple example.
We are going to print weekday
based on integer. 0
represents Sunday, 1
represents Monday
and so on..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.arpit.java2blog; public class SwitchCaseExample { public static void main(String[] args) { int dayOfWeek=5; switch(dayOfWeek) { case 0 : System.out.println("Sunday"); break; case 1 : System.out.println("Monday"); break; case 2 : System.out.println("Tuesday"); break; case 3 : System.out.println("Wednesday"); break; case 4 : System.out.println("Thrusday"); break; case 5 : System.out.println("Friday"); break; case 6 : System.out.println("Saturday"); break; // Default is executed when expression does not match with any of above conditions. default : // Optional System.out.println("Invalid day of week"); } } } |
Output:
As we have highlighted in syntax, break keyword is optional.
Let’s see what happens if we do not use break statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
package org.arpit.java2blog; public class SwitchCaseExample { public static void main(String[] args) { int dayOfWeek=5; switch(dayOfWeek) { case 0 : System.out.println("Sunday"); case 1 : System.out.println("Monday"); case 2 : System.out.println("Tuesday"); case 3 : System.out.println("Wednesday"); case 4 : System.out.println("Thrusday"); case 5 : System.out.println("Friday"); case 6 : System.out.println("Saturday"); // Default is executed when expression does not match with any of above conditions. default : // Optional System.out.println("Invalid day of week"); } } } |
Output:
Saturday
Invalid day of week
As you can see here, if you do not use break statement, it will execute all the statements once condition is met.
Switch case String example
You can use String also in expression from Java 7 onwards.
Let’s understand with help of simple example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
package org.arpit.java2blog; public class SwitchCaseExample { public static void main(String[] args) { int dayOfWeek=5; switch(dayOfWeek) { case 0 : System.out.println("Sunday"); break; case 1 : System.out.println("Monday"); break; case 2 : System.out.println("Tuesday"); break; case 3 : System.out.println("Wednesday"); break; case 4 : System.out.println("Thrusday"); break; case 5 : System.out.println("Friday"); break; case 6 : System.out.println("Saturday"); break; // Default is executed when expression does not match with any of above conditions. default : // Optional System.out.println("Invalid day of week"); } } } |
Output:
Switch case uses equals method comparison internally, so case statement is case sensitive here.
Choosing between Switch case
and if-else if it depends on readability and various factors. You can choose as per your needs.
that’s all about switch case in java.