🚀 Introduction: How Has the switch
Statement Changed Over the Years?
The switch
statement in Java has evolved significantly over the years, improving readability, flexibility, and reducing boilerplate code. Starting with Java 7, which introduced String-based switches, the evolution continued through Java 12's switch expressions, Java 17’s pattern matching, and Java 21's enhanced pattern matching.
✅ What’s New in Java’s switch
Statement?
✔ Java 7 – Allowed String
in switch
.
✔ Java 12 (Preview) – Introduced switch
expressions.
✔ Java 13 (Preview) – Improved switch
with yield
.
✔ Java 14 – Finalized switch
expressions.
✔ Java 17 – Introduced Pattern Matching in switch
(Preview).
✔ Java 21 – Finalized Pattern Matching in switch
.
📌 In this guide, you’ll learn:
✅ How switch
evolved from Java 7 to Java 21.
✅ The benefits of switch
expressions and pattern matching.
✅ How to write modern, concise, and powerful switch
statements.
1️⃣ Java 7: switch
Statement Introduces String Support
Before Java 7, the switch
statement only supported integral types (int
, char
, short
, byte
). Java 7 introduced String
support, making code cleaner and faster.
📌 Example: switch
with String
in Java 7
public class Java7SwitchExample {
public static void main(String[] args) {
String day = "MONDAY";
switch (day) {
case "MONDAY":
System.out.println("Start of the workweek");
break;
case "FRIDAY":
System.out.println("Weekend is near!");
break;
default:
System.out.println("Regular day");
}
}
}
✅ Key Benefits:
✔ More readable than using multiple if-else
statements.
✔ Faster execution than using equalsIgnoreCase()
.
2️⃣ Java 12: switch
Expressions (Preview Feature)
Java 12 introduced switch
expressions, which allowed switch
to return values without requiring break
statements.
📌 Example: switch
as an Expression in Java 12
public class Java12SwitchExample {
public static void main(String[] args) {
String day = "MONDAY";
String message = switch (day) {
case "MONDAY" -> "Start of the workweek";
case "FRIDAY" -> "Weekend is near!";
default -> "Regular day";
};
System.out.println(message);
}
}
✅ Key Benefits:
✔ No need for break
statements.
✔ More concise and expressive.
3️⃣ Java 13: yield
for Returning Values in switch
(Preview Feature)
Java 13 introduced the yield
keyword, allowing multi-line cases to return values.
📌 Example: Using yield
in Java 13
public class Java13SwitchExample {
public static void main(String[] args) {
String day = "MONDAY";
String message = switch (day) {
case "MONDAY" -> {
System.out.println("Executing case MONDAY");
yield "Start of the workweek";
}
case "FRIDAY" -> "Weekend is near!";
default -> "Regular day";
};
System.out.println(message);
}
}
✅ Key Benefits:
✔ Allows multi-line logic inside a switch
expression.
✔ More flexible and readable.
4️⃣ Java 14: switch
Expressions Finalized
With Java 14, switch
expressions were finalized and no longer needed to be enabled as a preview feature.
📌 Example: Concise switch
Expression in Java 14
public class Java14SwitchExample {
public static void main(String[] args) {
String result = switch ("FRIDAY") {
case "MONDAY" -> "Start of the workweek";
case "FRIDAY" -> "Weekend is near!";
default -> "Regular day";
};
System.out.println(result);
}
}
✅ Key Benefits:
✔ Officially available without preview flags.
✔ Fully supports yield
and ->
syntax.
5️⃣ Java 17: Pattern Matching in switch
(Preview Feature)
Java 17 introduced pattern matching in switch
, allowing switch
statements to match objects based on type instead of relying on instanceof
.
📌 Example: Pattern Matching in Java 17 (Preview)
public class Java17SwitchExample {
static String process(Object obj) {
return switch (obj) {
case String s -> "String: " + s;
case Integer i -> "Integer: " + i;
default -> "Unknown type";
};
}
public static void main(String[] args) {
System.out.println(process("Hello"));
System.out.println(process(100));
System.out.println(process(3.14));
}
}
✅ Key Benefits:
✔ Eliminates instanceof
checks.
✔ More readable and concise.
6️⃣ Java 21: Pattern Matching in switch
(Finalized Feature)
Java 21 finalized pattern matching in switch
statements, allowing for even more advanced control flows.
📌 Example: Advanced Pattern Matching in Java 21
public class Java21SwitchExample {
static void process(Object obj) {
switch (obj) {
case String s when s.length() > 5 -> System.out.println("Long String: " + s);
case String s -> System.out.println("Short String: " + s);
case Integer i when i > 100 -> System.out.println("Large Integer: " + i);
case Integer i -> System.out.println("Small Integer: " + i);
default -> System.out.println("Unknown type");
}
}
public static void main(String[] args) {
process("HelloWorld");
process("Hi");
process(150);
process(42);
}
}
✅ Key Benefits:
✔ when
Clauses – Add conditional logic within cases.
✔ Better Readability – No need for multiple if-else
statements.
🎯 Summary: How switch
Evolved in Java

switch
Evolved in Java📢 Stay Connected & Keep Learning! 🚀
🔗 Explore my Udemy courses: Java Guides Udemy Courses
📖 Read more tutorials on my blog: Java Guides
🎥 Watch free Java video tutorials on YouTube: Java Guides YouTube
Comments
Post a Comment
Leave Comment