JavaIntermediate#fundamentals#java14

What is the difference between switch statement and switch expression (Java 14+)?

A switch statement executes code blocks based on matching case labels and needs explicit break to avoid fall-through. A switch expression (with arrow syntax) returns a value directly, has no fall-through, and requires exhaustive coverage.

Example
int day = 3;
String name = switch (day) {
  case 1 -> "Mon";
  case 2 -> "Tue";
  default -> "Other";
};

Related Questions

1
JavaBeginner#strings#java15

What is text block syntax in Java (Java 15+)?

Open
2
JavaIntermediate#strings

What is the String pool (intern pool)?

Open
3
JavaBeginner#strings

How do you reverse a String in Java?

Open