Java Switch-Case Statements:
The switch Statement
Unlike if-then and if-then-else statements, the switch statement can have a number of possible execution paths. A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types (discussed in Enum Types), the String class, and a few special classes that wrap certain primitive types: Character, Byte, Short, and Integer .
Syntax of the Switch Statement
switch (expression) {
case value1:
// Statements to execute if expression == value1
break;
case value2:
// Statements to execute if expression == value2
break;
// More cases as needed
default:
// Statements to execute if none of the above cases match
}
Java case keyword
The case keyword is used within a switch statement to specify a particular value or range of values that the expression being switched on might have. When the expression matches a case value, the corresponding block of code associated with that case is executed.
Java default keyword
The default keyword in a switch statement provides a fallback option. It specifies a block of code to execute when none of the case values match the value of the expression. The default case is optional, but including it ensures that there is always a block of code to execute if no specific case matches.
Using Strings in switch Statements
In Java SE 7 and later, you can use a String object in the switch statement's expression
Examples of Switch Statement:
Example 1: Basic Switch Statement
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;
public class SwitchDemo1
{
public static void main(String[] args)
{
// Get the current date
LocalDate currentDate = LocalDate.now();
// Get the day of the week as an enum (MONDAY, TUESDAY, etc.)
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
// Get the numeric value of the day of the week (1 for Monday, 2 for Tuesday, etc.)
int dayOfWeekNumber = dayOfWeek.getValue();
// Print the day of the week number
System.out.println("Day of the week number: " + dayOfWeekNumber);
String weekday=null;
switch(dayOfWeekNumber)
{
case 0:
weekday="Sunday";
break;
case 1:
weekday="Monday";
break;
case 2:
weekday="Tuesday";
break;
case 3:
weekday="Wednesday";
break;
case 4:
weekday="Thursday";
break;
case 5:
weekday="Friday";
break;
case 6:
weekday="Saturday";
break;
default:
System.out.println("Wrong Day");
}
System.out.println("Week Day is..."+weekday);
}
}
Output:
Day of the week number: 4
Week Day is...Thursday
Using String in Switch
import java.time.DayOfWeek;
import java.time.LocalDate;
public class SwitchStringDemo {
public static void main(String[] args) {
// Get the current date
LocalDate currentDate = LocalDate.now();
// Get the day of the week as an enum (MONDAY, TUESDAY, etc.)
DayOfWeek dayOfWeek = currentDate.getDayOfWeek();
// Get the day of the week name in uppercase
String dayOfWeekName = dayOfWeek.toString().toUpperCase();
// Switch statement with uppercase comparisons
switch (dayOfWeekName) {
case "MONDAY":
System.out.println("Start of work week");
break;
case "TUESDAY":
case "WEDNESDAY":
case "THURSDAY":
System.out.println("Days to Work");
break;
case "FRIDAY":
System.out.println("End of work week");
break;
case "SATURDAY":
case "SUNDAY":
System.out.println("Weekend");
break;
default:
// Handle unexpected cases
System.out.println("Unknown day of the week");
}
}
}
Output:
Days to Work
Example 2: Using Switch with Enum
package demo;
public class SwitchEnum {
public static void main(String[] args) {
enum Direction {
UP, DOWN, LEFT, RIGHT
}
Direction dir = Direction.RIGHT;
String movement;
switch (dir) {
case UP:
movement = "Move up";
break;
case DOWN:
movement = "Move down";
break;
case LEFT:
movement = "Move left";
break;
case RIGHT:
movement = "Move right";
break;
default:
movement = "Invalid direction";
}
System.out.println("Movement: " + movement); // Output: Move right
}
}
Output:
Movement: Move right