Nearby lessons

99 of 125

Java - Switch Expressions

📌 What You Will Learn
  • Switch expressions (first preview)
  • String indent() and transform()
  • Collectors.teeing
  • Compact number formatting

Switch Expressions is a core concept of the Java language. This lesson explains Java 12 — The Switch Gets Modern, Old Switch vs New Switch Expression and Old Switch vs New Switch — Comparison with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Java 12 — The Switch Gets Modern

Java 12 (March 2019) is a small version but it started one of the most loved changes: making the old switch statement into a modern switch expression. (It was a preview here; it became final in Java 14.)

Old Switch vs New Switch Expression

The old switch is verbose and easy to break (forgetting break causes fall-through bugs). The new switch expression is short and safe:

Example02
JCode Cell
1// OLD - switch statement
2int days;
3switch (month) {
4 case 1:
5 case 3:
6 case 5:
7 case 7:
8 days = 31;
9 break;
10 case 2:
11 days = 28;
12 break;
13 default:
14 days = 30;
15}

Old Switch vs New Switch Expression

See the difference — the new style: no break, multiple values on one case (case 1, 3, 5, 7), and it returns a value directly into the variable. The arrow -> means if this case matches, produce this value.

In simple words: A switch expression is a switch that gives back a value. With the arrow -> you need no break, and the whole switch can be assigned to a variable in one line.
Example03
JCode Cell
1// NEW - switch expression (arrow style)
2int days = switch (month) {
3 case 1, 3, 5, 7 -> 31;
4 case 2 -> 28;
5 default -> 30;
6};

Old Switch vs New Switch — Comparison

PointOld switch statementNew switch expression
break needed?Yes (fall-through bugs)No
Returns a value?No (you assign inside cases)Yes — the whole switch gives a value
Multiple values per caseSeparate case linescase 1, 3, 5 ->
StyleColon :Arrow -> (or yield for blocks)
SinceJava 1.0Final in Java 14

Switch Expressions — Now with yield

Java 13 added a second style to switch expressions: the yield keyword for the block style (for cases with more than one statement):

Trainer's Note: Use -> value for simple cases. Use { ... yield value; } when a case needs several statements. yield returns a value from inside a block — this is different from return, which exits the whole method.
In simple words: `yield` hands a value back from inside a switch block. return would leave the whole method, but yield only ends that one case and gives the switch expression its result.
Example05
JCode Cell
1int days = switch (month) {
2 case 1, 3, 5, 7 -> 31; // arrow style (Java 12)
3 
4 case 2 -> { // block style - several lines
5 System.out.println("Checking leap year...");
6 yield 28; // yield gives back a value from a block
7 }
8 default -> 30;
9};

Switch Expressions — Now FINAL

Switch expressions, previewed in Java 12 and 13, became a standard, permanent feature in Java 14. No --enable-preview needed anymore.

In simple words: Final means you can use the feature without any special flag. Switch expressions are now a normal part of Java 14, so no --enable-preview is needed to compile them.
Example06
JCode Cell
1String result = switch (grade) {
2 case 'A' -> "Excellent";
3 case 'B' -> "Good";
4 case 'C' -> "Average";
5 default -> "Needs improvement";
6};
7System.out.println(result);
Output
Excellent
📝 Key Takeaways
  • Java 12 introduced switch expressions as a preview — no break, arrows, returns a value.
  • indent(n) adds/removes leading spaces on every line.
  • transform() chains String operations in one line.
  • Collectors.teeing() runs two collectors at once and merges results.
  • CompactNumberFormat shows big numbers as 1.2K, 3.5M.
  • Preview features are experiments that become final later.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4