The switch statement in Java picks one branch out of several based on the value of a single expression. The arrow form and the switch expressions turn that statement into a compact way to turn a value into a result instead of a stack of if and else assignments. Here is how each form behaves and where each one fits.
The previous lesson in this module covered "How Do if, else and Logical Operators Branch Your Code?", and the next lesson in this module is about "How Do for, while and do-while Loops Differ in Java?".
How does the classic switch statement work?
In its original form the switch takes one expression and it compares it against a list of labels. Each case label holds a constant value, and the body under a label runs when that value matches. The default label catches everything that did not match, and it is optional, though omitting it means that the unmatched input is silently ignored.
public class GradeSwitch {
public static void main(String[] args) {
String grade = "B";
switch (grade) {
case "A":
System.out.println("Outstanding");
break;
case "B":
System.out.println("Good");
break;
default:
System.out.println("Unknown grade");
}
}
}The output is a single line.
GoodTwo rules govern the original form. The value of the switch expression has to be a type that can be compared for equality, and that rules out the floating point types. And a break at the end of a case body stops the switch from carrying on into the next case. Neither label needs to be a literal, so you can switch on an enum constant or on a final variable as easily as on a string.
What is the arrow form of switch?
The arrow form replaces the colon and the break with an arrow between the label and the body. Each case then stands alone. It runs its body and it leaves. There is no implicit fall-through to reason about and there is no break to forget, and that removes the most common switch bug outright.
public class ArrowSwitch {
public static void main(String[] args) {
int day = 3;
switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
case 3 -> {
System.out.println("Wednesday");
System.out.println("Midweek");
}
default -> System.out.println("Unknown day");
}
}
}A block after the arrow is how you run more than one statement in a case, and because it is a real block you can declare the variables inside it without worrying about them clashing with another case.
Wednesday
MidweekWhy do the cases fall through, and how do I stop it?
In the original form a case body that does not end with a break simply continues into the next case. That is called fall-through, and it is occasionally deliberate. When you want to group several values that share one handler, you stack the labels and you put a single body underneath.
public class FallThroughDemo {
public static void main(String[] args) {
int level = 2;
switch (level) {
case 1:
System.out.println("notify the support inbox");
case 2:
System.out.println("write an audit entry");
case 3:
System.out.println("page the on-call engineer");
}
}
}With a level of 2 the output is this.
write an audit entry
page the on-call engineerThe first case was skipped because the value was 2, and the third case ran anyway because the second case had no break. That is the fall-through working exactly as it was designed, and also exactly as it bites the people who forget the break. In the arrow form the problem cannot occur, and that is why it is the better default for the new code.
What is a switch expression?
A switch expression is a switch that is used as a value rather than as a statement. It computes a result, and every path through it has to produce one. The arrow form makes this short, because a single-expression case is its own result.
public class ShippingCost {
public static void main(String[] args) {
int weightKg = 12;
int cost = switch (weightKg) {
case 0, 1 -> 500;
case 2, 3, 4 -> 700;
default -> 700 + (weightKg - 4) * 50;
};
System.out.println("Weight " + weightKg + " kg costs " + cost + " cents");
}
}The output is this.
Weight 12 kg costs 1100 centsAnd notice the comma in the case 0, 1, which lets one body cover several values without the stacking and the fall-through that the original form needed.
When a case needs more than one statement then you use a block and the yield keyword to name the value that the block produces.
public class YieldSwitch {
public static void main(String[] args) {
int tier = 3;
int price = switch (tier) {
case 1 -> 1000;
case 2 -> 2000;
default -> {
int base = 3000;
int loyalty = 250;
yield base + loyalty;
}
};
System.out.println("Price: " + price);
}
}That prints the price of 3250. The block assigns the two local values and yields the total, and because the whole thing is an expression you can assign it to a variable, you can pass it as an argument, and you can return it from a method.
Can switch match on types as well as values?
The switch can also branch on the type of the value rather than on its contents. The labels become the type patterns, and each one binds the matched value to a variable that you can use inside the case body.
public class TypeSwitch {
public static void main(String[] args) {
Object payment = 125.50;
String description = switch (payment) {
case Integer i -> "whole-number payment of " + i;
case Double d -> "decimal payment of " + d;
case String s -> "voucher code " + s;
default -> "unrecognised payment";
};
System.out.println(description);
}
}The output is "decimal payment of 125.5", because the value is a Double and the Double label comes before the default. The patterns are tested in order, so the most specific label has to be written first.
Quick reference
the form the best for watch out for
--------------------------- ------------------------------ ----------------------
the classic statement the code bases that already the fall-through without
use it a break
the arrow statement dispatching one value to one there is no fall-through,
handler so the grouping needs the
commas
the arrow expression turning a value into a result every path must produce a
value
the expression with a block the multi-step calculations it needs the yield inside
for each case the block
the type patterns handling several types in one the order matters, and the
place most specific goes first
Try it yourself: build a small order-status dispatcher. Give it the statuses of NEW, PAID, SHIPPED and CANCELLED, and use the arrow form to print a different message for each one plus a default for anything unknown. Then convert it into a switch expression that returns the number of days that a refund takes for each status, using a block and the yield for the CANCELLED case. Finally, write the same logic as an if and else chain and count the lines, because the switch is usually the shorter and the clearer of the two once you have more than three branches.