Table of Contents
If you’re a Java developer, you’re probably familiar with the switch
statement. It’s a powerful tool that allows you to execute different blocks of code based on the value of a variable. But do you really know all the ins and outs of this keyword? In this article, we’ll take a deep dive into the switch
statement and explore some of its lesser-known features.
Related Articles
Basic Syntax
Let’s start with the basics. The switch
statement takes a single expression as its argument, which is usually a variable or a method call that returns a value. It then compares the value of this expression to a list of possible values, each of which is associated with a block of code to be executed if the expression matches that value.
Here’s an example:
int dayOfWeek = 3;
switch (dayOfWeek) {
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;
default:
System.out.println("Weekend");
}
In this example, we’re using a switch
statement to print the name of the day of the week based on the value of the dayOfWeek
variable. Since dayOfWeek
is equal to 3, the code inside the case 3:
block will be executed, and the output will be:
Wednesday
Notice that we’ve included a default:
block at the end of the switch
statement. This block will be executed if none of the other cases match the value of the expression. In this example, if dayOfWeek
had a value other than 1-5, the output would be:
Weekend
Multiple Cases
One of the lesser-known features of the switch
statement is the ability to group multiple cases together. This can be useful when you want to execute the same block of code for multiple values. Here’s an example:
int month = 2;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
System.out.println("31 days");
break;
case 4:
case 6:
case 9:
case 11:
System.out.println("30 days");
break;
case 2:
System.out.println("28 or 29 days");
break;
default:
System.out.println("Invalid month");
}
In this example, we’re using a switch
statement to print the number of days in a given month. Notice that we’ve grouped the months with 31 days together, and the months with 30 days together. This makes the code more concise and easier to read.
String Cases
Another lesser-known feature of the switch
statement is the ability to use strings as cases. This can be useful when you’re working with user input or other string-based data. Here’s an example:
String color = "red";
switch (color) {
case "red":
System.out.println("Roses are red");
break;
case "blue":
System.out.println("Violets are blue");
break;
case "green":
System.out.println("Grass is green");
break;
default:
System.out.println("I don't know that color");
}
In this example, we’re using a switch
statement to print a poem based on the color entered by the user. Notice that we’re using string literals as the cases, surrounded by double quotes.
Enhanced Switch
Starting with Java 12, the switch
statement has been enhanced with a new syntax that allows you to use expressions as the cases, and to return a value from the switch
statement. Here’s an example:
int dayOfWeek = 3;
String dayName = switch (dayOfWeek) {
case 1 -> "Monday";
case 2 -> "Tuesday";
case 3 -> "Wednesday";
case 4 -> "Thursday";
case 5 -> "Friday";
default -> "Weekend";
};
System.out.println(dayName);
In this example, we’re using the new enhanced switch
syntax to assign the name of the day of the week to the dayName
variable. Notice that we’re using the arrow (->
) syntax to associate each case with a value. We’re also using the break
keyword, which is optional in this syntax.
One of the benefits of the enhanced switch
syntax is that it can be used as an expression, which means that it can return a value. In this example, the switch
statement returns the name of the day of the week, which is then assigned to the dayName
variable.
Conclusion
The switch
statement is a powerful tool that can make your code more concise and easier to read. By using multiple cases, string cases, and the enhanced switch
syntax, you can take full advantage of this keyword and write more efficient and effective code.
Remember: Always include a default:
block in your switch
statement, and group related cases together for easier readability.
Don’t forget: The enhanced switch
syntax is available starting with Java 12, so make sure to update your code if you’re using an older version of Java.
Finally: Keep learning and stay up-to-date with the latest Java features and updates. Check out our article on Java 17: The Latest and Greatest Features for more information.
The post Switching it up: A comprehensive guide to the Switch keyword in Java appeared first on Java Master.