What is a switch Statement?
A switch statement is used to execute one block of code based on the value of an expression.
Instead of checking multiple conditions using if–else, we can use switch when:
- We compare one value
- Against multiple fixed values
Data Types Supported in switch
switch supports:
-
byte,short,int,char String
(Other types like long, float, double, and boolean are not used here.)
=> Usage of break in switch
break is used to stop the execution of a switch case once a matching case is found.
- Without
break, Java will continue executing the next cases (called fall-through) - With
break, Java exits theswitchblock immediately
In simple words:
breaktells Java: “Stop here and come out of the switch.”
Traditional switch Case (Using break)
This is the basic and commonly used form of switch.
=> Example:
String mark = "Karthick";
switch (mark) { // Expression
case "abc":
System.out.println("A grade");
break;
case "xyz":
System.out.println("B grade");
break;
case "asd":
System.out.println("C grade");
break;
case "klo":
System.out.println("D grade");
break;
default:
System.out.println("Fail");
}
=> Explanation:
- The
switchchecks the value ofmark - If it matches a
case, that block runs -
breakstops execution from falling into the next case -
defaultruns when no case matches
=> Usage of default in switch
default is executed when none of the case values match the switch expression.
- It acts like an
elsecondition - It is optional but recommended
In simple words:
defaulthandles unexpected or unmatched values.
=> Enhanced switch Case (Arrow -> Style)
Java also provides a simpler and cleaner syntax using the arrow (->).
=> Example:
String mark = "Karthick";
switch (mark) {
case "abc" -> System.out.println("A grade");
case "xyz" -> System.out.println("B grade");
case "asd" -> System.out.println("C grade");
case "klo" -> System.out.println("D grade");
default -> System.out.println("Fail");
}
=> Benefits:
- No need to write
break - Code is shorter and more readable
- Reduces mistakes
=> switch as an Expression (Returning a Value)
In modern Java, switch can also return a value and store it in a variable.
=> Example:
String result = switch (mark) {
case "abc" -> "A grade";
case "xyz" -> "B grade";
case "asd" -> "C grade";
case "klo" -> "D grade";
default -> "Fail";
};
Here:
- The
switchreturns a value - The result is stored in the variable
result - This makes the code more compact and clean
When to Use switch
Use switch when:
- You compare one variable
- With multiple known values
- You want better readability than long
if–elsechains
➡️ When Was switch Introduced in Java?
- The
switchstatement was introduced in Java 1.0 - Year: 1996
At that time, switch supported only:
byteshortintchar
➡️ When Did Java Allow String in switch?
-
Stringwas allowed insideswitchfrom Java 7 - Year: 2011
From Java 7 onwards, we can write:
switch (name) {
case "abc":
System.out.println("A grade");
}
➡️ When was Arrow introduced in Java?
- Enhanced
switchwith->(arrow syntax) came in Java 14 - It removed the need for
breakand made code cleaner
📝 Short-Note
-
break→ exits the switch -
default→ runs when no case matches -
switchintroduced → Java 1.0 (1996) -
Stringin switch → Java 7 (2011)
Top comments (0)