Switch ... Case
statement is a popular selection statement used in Java programming. Switch-case simply set cases and related code block to be executed when given case match.
Syntax
The complete syntax of the switch case statement in Java is like below.
switch(EXPRESSION){ case CASE1: CASE1_CODE_BLOCK; break; case CASE2: CASE2_CODE_BLOCK; break; ... default: DEFAULT_CODE_BLOCK; }
- `switch` is the keyword used to create a new switch-case block.
- `EXPRESSION` is the expression which will be checked against all cases like CASE1, CASE2 … except for the default case.If it is matched the related case block will be executed.
- `case CASE1` defines a case and provide the case code block to be executed if the case is matched with the EXPRESSION.
- `CASE1_CODE_BLOCK` will be executed if the CASE1 is matched with the EXPRESSION.
- `break;` statement will end the current case and the whole case-switch which means the following cases will not be checked. `break;` statement is optional but it is generally used as a good practice.
- `default` is the default case if there is no match the default case will be executed with the `DEFAULT_CODE_BLOCK`
- `DEFAULT_CODE_BLOCK` is executed when the default case matched.
Switch Case Example
We will create an example we will print the weekday name according to the given day number.
public class Switch_Case_Example { public static void main(String[] args) { int day = 5; switch (day) { 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("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; } } }

We can see that the variable named day
stores the day number and checked against other day numbers from 1 to 7. The day number matches the case 5
which is Friday and it will be printed to the screen. The
breakstatement will end the current switch-case.
Friday
<h1>Switch Case String Example</h1>
We can also use string expression in order to match the given cases. We will check in the reverse form of the previous example. We will provide theday name and print the day number to the console. If we are using String for math under the hood the String type
equals()` method will be used by the switch-case.
public class Switch_Case_Example_String { public static void main(String[] args) { String day_name = "Friday" ; switch (day_name) { case "Monday": System.out.println(1); break; case "Tuesday": System.out.println(2); break; case "Wednesday": System.out.println(3); break; case "Thursday": System.out.println(4); break; case "Friday": System.out.println(5); break; case "Saturday": System.out.println(6); break; case "Sunday": System.out.println(7); break; } } }

This code will print 5 to the console because the “Friday” is the 5th day of the week. We will set the day_name
variable as “Friday” as a string and check with the switch-case.
Switch Case Enum Example
We can also use enum type data for switch-case. We will create an enum type Day
and create a new variable with the Day enum type named Now
. Then we will set Day.Friday
to the Now
.
public class Switch_Case_Example_Enum { public enum Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } public static void main(String args[]) { Day Now = Day.Friday; switch (Now) { case Sunday: System.out.println("Sunday"); break; case Monday: System.out.println("Monday"); break; case Tuesday: System.out.println("Tuesday"); break; case Wednesday: System.out.println("Wednesday"); break; case Thursday: System.out.println("Thursday"); break; case Friday: System.out.println("Friday"); break; case Saturday: System.out.println("Saturday"); break; } } }

Switch Case Rules
While using switch-case there are some rules to obey and work the switch-case properly.
- The duplicate case is not allowed which will conflict and give an error.
- The expression and case values should be the same type in order to check, compare and match properly.
- Case values should be constant or literal (String or Number). Variables are not allowed.
- The break statement is optional where it can be omitted for the whole switch-case.
- When the break statement is reached the whole switch-case will end.
Break Keyword
We have used all of our previous examples the break
keyword. The break keyword is used to stop the switch-case and then exit from the switch-case which will prevent to check the following cases. This can be useful because after a single match we may do not need the next checks. If we forget the break statement the underneath code blocks will be executed.
Default Keyword/Case
The default case of the keyword is used when there is no match for the given cases and we want to run some code. The default keyword also named default case which simply means run default case where there is no match. This can be especially useful to run a default code. We can put the default case anywhere in the switch-case but in order to make the code more readable putting default to the end of the switch-case is a best practice.
public class Switch_Case_Example { public static void main(String[] args) { int day = 50; switch (day) { 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("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); break; default: System.out.println("Please provide proper day number"); } } }

In the example, the day
is 50 and this will not match in the given cases so the default case will be run which will print to the screen “Please provide proper day number”.
Nested Switch Case
switch-case can be nested in multiple times. But keep in mind that this will make the application mess and harder to read other programmers. In the following example, we will add a nested switch-case to the “Sunday” case.
public class Switch_Case_Example { public static void main(String[] args) { int day = 50; String time = "Morning"; switch (day) { 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("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; case 7: System.out.println("Sunday"); switch(time){ case "Morning": System.out.println("Morning"); break; case "Afternoon": System.out.println("Afternoon"); break; } break; default: System.out.println("Please provide proper day number"); } } }
