Java Switch Expressions

Introduction

Java introduced Switch Expressions as a preview feature in Java 12, enhanced them in Java 13, and officially finalized them in Java 14. Switch Expressions allow developers to write more concise, readable, and less error-prone code by enabling the switch construct to return values directly.

Switch Expressions are one of the major modern Java language improvements and work seamlessly with other features such as Pattern Matching, Records, and Sealed Classes in Java 17–21.

What are Java Switch Expressions?

A Switch Expression is an enhanced version of the traditional switch statement that can return a value. Instead of assigning values inside each case and using multiple break statements, you can write concise expressions using the -> arrow syntax.

For example, instead of writing:


int day = 2;
String name;
switch (day) {

   case 1:


name = "Monday";
       break;

   case 2:


 name = "Tuesday";
       break;
   default:
       name = "Unknown";
}

You can simply write:


int day = 2;
String name = switch (day) {
   case 1 -> "Monday";
   case 2 -> "Tuesday";
   default -> "Unknown";
};

The code is shorter, cleaner, and eliminates accidental fall-through.

Why Use Switch Expressions?

Switch Expressions provide several benefits:

  • Less boilerplate code
  • No need for break statements
  • No accidental fall-through
  • Can directly return values
  • More readable syntax
  • Supports multiple labels
  • Supports yield keyword for complex logic
  • Better integration with modern Java features

Syntax

Basic Syntax


variable = switch (expression) {
   case value1 -> result1;
   case value2 -> result2;
   default -> defaultResult;
};

Syntax Using yield


String result = switch (value) {
   case 1 -> "One";
   case 2 -> {
       System.out.println("Processing...");
       yield "Two";
   }
   default -> "Unknown";
};

The yield keyword returns a value from a block.

Traditional Switch vs Switch Expression

Traditional Switch


int day = 3;
String result;
switch (day) {

   case 1:


result = "Monday";
       break;

   case 2:


 result = "Tuesday";
       break;

case 3:


 result = "Wednesday";
       break;

   default:


       result = "Invalid";
}
System.out.println(result);

Switch Expression


int day = 3;
String result = switch (day) {
   case 1 -> "Monday";
   case 2 -> "Tuesday";
   case 3 -> "Wednesday";
   default -> "Invalid";
};
System.out.println(result);

Output

Wednesday

Example 1: Grade Evaluation


public class Main {
   public static void main(String[] args) {
       char grade = 'A';
       String message = switch (grade) {
           case 'A' -> "Excellent";
           case 'B' -> "Very Good";
           case 'C' -> "Good";
          case 'D' -> "Average";
           default -> "Fail";
       };
       System.out.println(message);
   }
}

Output

Excellent

Example 2: Calculator


public class Main {
   public static void main(String[] args) {
       char operator = '+';
       int a = 20;
       int b = 10;
       int result = switch (operator) {
           case '+' -> a + b;
           case '-' -> a - b;
          case '*' -> a * b
           case '/' -> a / b;
           default -> 0;
       };
       System.out.println(result);
   }
}

Output

30

Example 3: Multiple Case Labels

Java Switch Expressions allow multiple labels in one case


String season = switch (month) {
   case 12, 1, 2 -> "Winter";
   case 3, 4, 5 -> "Summer";
   case 6, 7, 8 -> "Rainy";
   case 9, 10, 11 -> "Autumn";
   default -> "Invalid";
};
System.out.println(season);

Output

Summer

Example 4: Using yield

When more than one statement is needed inside a case, use yield.


it number = 10;
String result = switch (number) {
   case 5 -> "Five";
   case 10 -> {
       System.out.println("Number Found");
       yield "Ten";
   }
   default -> "Unknown";
};
System.out.println(result);

Output

Number Found
Ten

Example 5: Enum with Switch Expression


enum Status {
   NEW,
   PROCESSING,
   COMPLETED
}
public class Main {
   public static void main(String[] args) {
       Status status = Status.PROCESSING;
       String message = switch (status) 
           case NEW -> "Order Received";
           case PROCESSING -> "Preparing Order"
           case COMPLETED -> "Delivered";
       };
       System.out.println(message);
   }
}

Output

Preparing Order

Switch Expressions with Pattern Matching

Java 21 combines Switch Expressions with Pattern Matching.


Object obj = 100;
String result = switch (obj) {
   case Integer i -> "Integer : " + i;
   case String s -> "String : " + s
   default -> "Unknown";
};
System.out.println(result);

Output

Integer : 100

This makes handling multiple object types much simpler

Real-Life Example

Imagine an online food delivery application.


String payment = "UPI";
String message = switch (payment) 
   case "UPI" -> "Processing UPI Payment";
   case "CARD" -> "Processing Card Payment";
   case "COD" -> "Cash on Delivery";
   default -> "Invalid Payment";
};
System.out.println(message);

Output

Processing UPI Payment

Switch Expressions make such business logic cleaner and easier to maintain.

Features of Switch Expressions

1. Returns Values

Unlike traditional switch statements, Switch Expressions directly return a value.

2. Arrow Syntax (->)

The arrow syntax removes the need for break.

Example


case 1 -> "One";

3. No Fall-Through

Traditional switches could accidentally execute multiple cases if break was omitted.

Switch Expressions eliminate this issue completely.

4. Multiple Labels

You can combine multiple values.


case 1, 2, 3 -> "Small";

5. Supports yield

Complex logic can return values using yield.

6. Exhaustive Cases

Switch Expressions require all possible cases to be covered.

This usually means including a default case unless switching on an enum or a sealed hierarchy where all values are explicitly handled.

Traditional Switch vs Switch Expression

Feature raditional Switch Switch Expression
Returns value No Yes
Requires break Yes No
Fall-through Yes No
Arrow syntax No Yes
Multiple labels Limited Yes
Supports yield No Yes

Best Practices

  • Prefer Switch Expressions for returning values.
  • Use arrow (->) syntax whenever possible.
  • Use yield only when multiple statements are needed.
  • Cover all possible cases.
  • Use enums instead of magic numbers where appropriate.
  • Combine with Pattern Matching in Java 21 for cleaner type-based logic.
  • Keep each case concise and focused.

Common Mistakes

1. Forgetting Default Case

Wrong


String result = switch (value) {
   case 1 -> "One";
};

Correct


String result = switch (value) {
   case 1 -> "One";
   default -> "Unknown";
};

2. Using break Instead of Arrow

Wrong

case 1:


   break;

Correct


case 1 -> "One";

3. Forgetting yield in Block

Wrong


case 1 -> "One";case 10 -> {
   System.out.println("Ten");
}

Correct


case 10 -> {
   System.out.println("Ten");
   yield "Ten";
}

4. Mixing Old and New Styles Incorrectly

Avoid mixing traditional case: labels with -> syntax within the same switch expression, as it reduces readability and can lead to confusion.

When Should You Use Switch Expressions?

Switch Expressions are ideal when:

  • Returning a value based on multiple conditions.
  • Working with enums.
  • Replacing long if-else chains.
  • Building modern Java applications.
  • Using Java 14 or later.
  • Working with Pattern Matching and Sealed Classes.

Conclusion

Java Switch Expressions are a modern enhancement that makes conditional logic more concise, expressive, and reliable. By allowing switches to return values directly, removing the need for break statements, and introducing the -> arrow syntax and yield keyword, they greatly simplify everyday programming tasks.

When combined with modern Java features such as Pattern Matching, Records, and Sealed Classes, Switch Expressions help developers write cleaner and more maintainable applications.

Related Java Tutorials