Java Interview Questions
Java
Web DevelopmentBackendQuestion 9
How does the switch statement work in Java?
Answer:
The switch
statement in Java is a control flow statement that allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
Syntax
switch (expression) {
case value1:
// code to be executed if expression == value1
break; // optional
case value2:
// code to be executed if expression == value2
break; // optional
// you can have any number of case statements
default:
// code to be executed if none of the cases match
}
Key Points
- Expression: The expression in the
switch
statement must evaluate to a byte, short, int, char, String (since Java 7), or an enum type. - Case: Each
case
value must be a constant or a literal. Variables are not allowed. - Break: The
break
statement is used to terminate acase
in the switch statement. If omitted, execution will continue to the next case. - Default: The
default
case is optional and can be used for executing code when none of the cases match. It is similar to theelse
clause in anif
statement.
Example
public class SwitchExample {
public static void main(String[] args) {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
break;
}
System.out.println("The day is " + dayName); // Output: The day is Tuesday
}
}
Without Break
If the break
statement is omitted, Java will continue executing the subsequent cases until it encounters a break
or the end of the switch
statement. This is known as "fall-through."
public class SwitchExampleWithoutBreak {
public static void main(String[] args) {
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Sunday";
case 2:
dayName = "Monday";
case 3:
dayName = "Tuesday";
case 4:
dayName = "Wednesday";
case 5:
dayName = "Thursday";
case 6:
dayName = "Friday";
case 7:
dayName = "Saturday";
default:
dayName = "Invalid day";
}
System.out.println("The day is " + dayName); // Output: The day is Invalid day
}
}
Switch with Strings (Java 7 and Later)
Since Java 7, the switch
statement can also be used with strings.
public class SwitchWithString {
public static void main(String[] args) {
String day = "Tuesday";
String dayType;
switch (day) {
case "Monday":
case "Tuesday":
case "Wednesday":
case "Thursday":
case "Friday":
dayType = "Weekday";
break;
case "Saturday":
case "Sunday":
dayType = "Weekend";
break;
default:
dayType = "Invalid day";
break;
}
System.out.println(day + " is a " + dayType); // Output: Tuesday is a Weekday
}
}
Switch with Enums
Using enums with switch
statements is also common and provides a type-safe way to work with predefined constant sets.
public class SwitchWithEnum {
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
Day day = Day.TUESDAY;
String dayType;
switch (day) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THURSDAY:
case FRIDAY:
dayType = "Weekday";
break;
case SATURDAY:
case SUNDAY:
dayType = "Weekend";
break;
default:
dayType = "Invalid day";
break;
}
System.out.println(day + " is a " + dayType); // Output: TUESDAY is a Weekday
}
}
Conclusion
The switch
statement in Java provides a way to branch execution based on the value of an expression. By using switch
, you can make your code more readable and manage multiple possible execution paths more efficiently than with multiple if-else
statements.