Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Java Switch-Case Statement Tutorial
In Java, the switch
statement is a multi-branch control structure that allows you to choose a block of code to execute based on the value of a variable or expression. The switch
statement is often used as an alternative to a series of nested if-else
statements.
The switch
statement is followed by an expression in parentheses and a block of code enclosed in curly braces. Inside the block, you define case
labels followed by a constant value and a colon. The break
statement is used to exit the switch
block after a matching case
is executed.
switch (expression) { case constant1: // Code block to execute if expression == constant1 break; case constant2: // Code block to execute if expression == constant2 break; ... default: // Code block to execute if no matching case is found }
public class Main { public static void main(String[] args) { int day = 3; 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("Invalid day"); } } }
Output:
Wednesday
String
and enum
:In addition to primitive types like int
, char
, and byte
, Java supports using String
and enum
types in switch
statements.
Example with String
:
public class Main { public static void main(String[] args) { String day = "Tuesday"; switch (day) { case "Monday": System.out.println("It's Monday"); break; case "Tuesday": System.out.println("It's Tuesday"); break; // ... other cases default: System.out.println("Invalid day"); } } }
Example with enum
:
enum Weekday { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } public class Main { public static void main(String[] args) { Weekday day = Weekday.WEDNESDAY; switch (day) { case MONDAY: System.out.println("It's Monday"); break; case TUESDAY: System.out.println("It's Tuesday"); break; // ... other cases default: System.out.println("Invalid day"); } } }
Using switch-case in Java:
The switch
statement is used to make decisions based on the value of an expression. It allows for multiple conditions to be evaluated in a more concise manner.
int dayOfWeek = 2; switch (dayOfWeek) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; // ... cases for other days default: System.out.println("Invalid day"); }
Switch statement with primitive types in Java:
The switch
statement can be used with byte, short, char, int, and their wrapper classes (Byte, Short, Character, Integer).
int number = 2; switch (number) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; // ... other cases }
Handling multiple cases in Java switch: Multiple cases can be handled by allowing them to fall through, or by grouping them using a common block.
int dayOfWeek = 2; switch (dayOfWeek) { case 1: case 2: case 3: case 4: case 5: System.out.println("Weekday"); break; case 6: case 7: System.out.println("Weekend"); break; default: System.out.println("Invalid day"); }
Switch-case with enum in Java:
Enums are often used with switch
statements to represent a set of constant values.
enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } Day day = Day.MONDAY; switch (day) { case MONDAY: System.out.println("It's Monday!"); break; // ... other cases }
Nested switch statements in Java: Switch statements can be nested to handle complex scenarios where multiple conditions need to be evaluated.
int outerValue = 1; int innerValue = 2; switch (outerValue) { case 1: switch (innerValue) { case 1: System.out.println("Outer 1, Inner 1"); break; case 2: System.out.println("Outer 1, Inner 2"); break; } break; // ... other cases }
String in switch statement in Java:
Starting from Java 7, the switch
statement supports String
values.
String dayOfWeek = "Monday"; switch (dayOfWeek) { case "Monday": System.out.println("It's Monday!"); break; // ... other cases }
Default case in Java switch statement:
The default
case is executed when none of the preceding cases match the value of the expression.
int dayOfWeek = 8; switch (dayOfWeek) { case 1: System.out.println("Monday"); break; // ... other cases default: System.out.println("Invalid day"); }
Java switch-case and break statement:
The break
statement is used to exit the switch
statement. Without it, execution would fall through to subsequent cases.
int dayOfWeek = 2; switch (dayOfWeek) { case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); // No break, falls through to the next case case 3: System.out.println("Wednesday"); break; // ... other cases }
Switch-case for char data type in Java:
The switch
statement can be used with the char
data type.
char grade = 'A'; switch (grade) { case 'A': System.out.println("Excellent"); break; // ... other cases }
Switch statement for multiple conditions in Java:
The switch
statement is effective when there are multiple conditions based on the value of a variable.
int value = 2; switch (value) { case 1: System.out.println("One"); break; case 2: System.out.println("Two"); break; // ... other cases }