Sitemap

Switch statement evoluation

7 min readMay 30, 2026

--

1. Switch Statement (Traditional)

Definition

  • Basic control flow introduced from early Java versions.
  • Executes a block based on matching case.

Syntax

int day = 2;
switch(day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
default:
System.out.println("Invalid");
}

Characteristics

  • Uses break to avoid fall-through
  • Supports primitives, enums, and String (Java 7+)
  • Verbose, error-prone (missing break → bugs)

2. Switch Expression (Java 12+ → stable in Java 14)

Definition

  • Modern version that returns a value
  • More concise and safer

Syntax

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

Characteristics

  • No break required
  • No fall-through
  • Returns value directly
  • Cleaner and functional style

3. Enhanced Switch (Java 12+ / 14+)

Definition

  • Improved syntax for switch (both statement + expression)
  • Introduces -> (lambda-style arrows)

Forms

a) Enhanced Switch Statement (no return)

switch (day) {
case 1 -> System.out.println("Monday");
case 2 -> System.out.println("Tuesday");
default -> System.out.println("Invalid");
}

b) Enhanced Switch Expression (with return)

String result = switch (day) {
case 1 -> "Monday";
case 2 -> "Tuesday";
default -> "Invalid";
};

c) Multiple labels

switch(day) {
case 1, 2, 3 -> System.out.println("Weekday");
default -> System.out.println("Other");
}

d) Block with yield

String result = switch(day) {
case 1 -> {
System.out.println("Processing…");
yield "Monday";
}
default -> "Invalid";
};

Quick Comparison

Press enter or click to view image in full size

Key Differences (Interview Answer)

Switch Statement vs Expression

  • Statement → executes logic
  • Expression → returns value

Traditional vs Enhanced

  • Traditional → case: break
  • Enhanced → case -> (clean & safe)

Best Practice (Java 17 / 21)

Get Syed Nazeer’s stories in your inbox

Join Medium for free to get updates from this writer.

Always prefer:

  • Switch expressions
  • Enhanced switch (->)

Avoid:

  • Old switch with fall-through unless required

One-line Summary

  • Switch Statement → old, imperative
  • Switch Case → part of switch
  • Switch Expression → returns value
  • Enhanced Switch → modern syntax with ->

If you want, I can also show: Pattern matching switch (Java 21)

Real-world design example (like grading or strategy pattern replacement)

Switch statement vs Switch expression vs Enhanced Switch

Let’s use one common example (grading system) and show it using:

  • Traditional switch case
  • switch expression (Java 14+)
  • Enhanced switch (modern arrow syntax)

Example: Grade based on the marks range

We’ll classify marks into:

  • >= 90 → Distinction
  • >= 75 → First Class
  • >= 50 → Pass
  • < 50 → Fail

1️Traditional switch case (old style)

public static String getGrade(int marks) {
int category = marks / 10;
switch (category) {
case 9:
case 10:
return "Distinction";
case 7:
case 8:
return "First Class";
case 5:
case 6:
return "Pass";
default:
return "Fail";
}
}

Key points:

  • Uses case + break/return
  • Fall-through is possible
  • Verbose

2️Switch Expression (Java 14+)

public static String getGrade(int marks) {
int category = marks / 10;
return switch (category) {
case 9, 10 -> "Distinction";
case 7, 8 -> "First Class";
case 5, 6 -> "Pass";
default -> "Fail";
};
}

Key points:

  • Returns value directly
  • No break needed
  • Cleaner and safer

3️⃣ Enhanced Switch (Pattern + Logic handling)

Here we directly apply logic (more readable than manipulating category):

public static String getGrade(int marks) {
return switch (marks) {
case int m when m >= 90 -> "Distinction";
case int m when m >= 75 -> "First Class";
case int m when m >= 50 -> "Pass";
default -> "Fail";
};
}

🔹 Key points:

  • Uses pattern matching (when)
  • More natural logic expression
  • No need for workaround like marks / 10

Quick Interview Summary

Press enter or click to view image in full size

Best Answer (Interview One-Liner)

Traditional switch uses case and break, switch expressions return values using ->, and enhanced switch adds pattern matching and guards, making code more concise and expressive.

/*
Demo of Switch statement vs Switch Expression vs Enhanced Switch
*/
public class SwitchDemoFinal {

public static void main(String[] args) {

Operation operation = new Addition();
// Operation operation = new Subtraction();
// Operation operation = new Multiplication();
// Operation operation = new Division();

System.out.println(switchStmtDemo(operation));
System.out.println(switchExpDemo(operation));
System.out.println(enhancedSwitchDemo(operation));
}

/*
Switch expression with pattern matching Java 17+
Pattern Matching: Operation object is instance Addition or Subtraction or Multiplication or Division will be decided by Switch
Object binding: Once Pattern matching done corresponding class object will be bound to switch case variable.
Return statement: Switch expression can directly be written with return statement.
Arrow(->): Arrow symbol be used to define Switch case action.
No break statement required.
*/
private static double enhancedSwitchDemo(Operation operation) {
return switch (operation) {
case Subtraction s -> s.calculation(20, 10);
case Addition r -> r.calculation(20, 10);
case Multiplication m -> m.calculation(20, 10);
case Division d -> d.calculation(20, 10);
default -> throw new IllegalStateException("Unexpected value: " + operation);
};
}

/*
Switch expression with pattern matching Java 12+
Return statement: Switch expression can directly be written with return statement.
Arrow(->): Arrow symbol be used to define Switch case action.
No break statement is required
*/
private static double switchExpDemo(Operation operation) {
String ops = null;
Addition addition = null;
Subtraction subtraction = null;
Multiplication multiplication = null;
Division division = null;
if (operation instanceof Addition) {
addition = (Addition) operation;
ops = "ADD";
} else if (operation instanceof Subtraction) {
subtraction = (Subtraction) operation;
ops = "SUB";
} else if (operation instanceof Multiplication) {
multiplication = (Multiplication) operation;
ops = "MUL";
} else if (operation instanceof Division) {
division = (Division) operation;
ops = "DIV";
}
return switch (ops) {
case "ADD" -> addition.calculation(20, 10);
case "SUB" -> subtraction.calculation(20, 10);
case "MUL" -> multiplication.calculation(20, 10);
case "DIV" -> division.calculation(20, 10);
default -> throw new IllegalStateException("Unexpected value: " + ops);
};
}

/*
Switch statement Java 7+
Traditional Switch case works with primitive and enum types only
however from Java7 onwards support for String has included.
break/return statement is required in each switch case.
*/
private static double switchStmtDemo(Operation operation) {
String ops = null;
double result = 0.0;
Addition addition = null;
Subtraction subtraction = null;
Multiplication multiplication = null;
Division division = null;
if (operation instanceof Addition) {
addition = (Addition) operation;
ops = "ADD";
} else if (operation instanceof Subtraction) {
subtraction = (Subtraction) operation;
ops = "SUB";
} else if (operation instanceof Multiplication) {
multiplication = (Multiplication) operation;
ops = "MUL";
} else if (operation instanceof Division) {
division = (Division) operation;
ops = "DIV";
}
switch (ops) {
case "ADD":
result = addition.calculation(20, 10);
break;
case "SUB":
result = subtraction.calculation(20, 10);
break;
case "MUL":
result = multiplication.calculation(20, 10);
break;
case "DIV":
result = division.calculation(20, 10);
break;
default:
throw new IllegalStateException("Unexpected value: " + ops);
}
return result;
}
}
/*
Key Characteristics:
--------------------
Matches constant values only
Uses break (or fall-through behavior)
Limited type support (traditional: int, enum, string)
No deep inspection of objects
Verbose and error-prone (missing break issues)
*/
public class Legacy_Switch_Example {
public static void main(String[] args) {
System.out.println(check1("SUN"));
System.out.println(check1("MON"));
System.out.println("----------------");
System.out.println(check2(1));
System.out.println(check2(4));
System.out.println("----------------");
check3(OrderStatus.NEW);
check3(OrderStatus.PROCESSING);
check3(OrderStatus.CANCELLED);
System.out.println("----------------");
System.out.println(check4("firstClass"));
System.out.println(check4("fail"));
}

//Switch statement
private static String check4(String grade) {
switch (grade.toLowerCase()) {
case ("firstclass"):
return "Above 60%";
case ("secondclass"):
return "40% to 59%";
case ("thirdclass"):
return "30% to 39%";
default:
return "Below 30%";
}
}

//Switch statement
private static String check1(String day) {
switch (day) {
case "MON":
case "TUE":
case "WED":
case "THU":
case "FRI":
return "Weekday";
case "SAT":
case "SUN":
return "Holiday";
default:
return "Invalid";
}
//Enhanced switch expression (Java 12+) - not a pattern matching
/*
return switch (day) {
case "MON", "TUE", "WED", "THU", "FRI" -> "Weekday";
case "SAT", "SUN" -> "Holiday";
default -> "Invalid";
};
*/
}

//Switch statement using int
private static String check2(int num) {
switch (num) {
case 1:
return "FIRST";
case 2:
return "SECOND";
case 3:
return "THIRD";
default:
return "ZERO";
}
}

/*
Enum-based switch improves readability, type safety,
and eliminates errors compared to string/int switches.
*/
private static void check3(OrderStatus status) {
switch (status) {
case NEW:
System.out.println("Order placed");
break;
case PROCESSING:
System.out.println("Order is being processed");
break;
case SHIPPED:
System.out.println("Order shipped");
break;
case DELIVERED:
System.out.println("Order delivered");
break;
case CANCELLED:
System.out.println("Order cancelled");
break;
default:
System.out.println("Unknown status");
}

}
}

public class SwitchPatternExample {

public static void main(String[] args) {
System.out.println(check1(10));
System.out.println(check1("Hello"));
System.out.println(legacyApproach1(10));
System.out.println(legacyApproach1("Hello"));
System.out.println("----------------------------");
System.out.println(check2(OrderStatus.NEW));
System.out.println(check2(OrderStatus.CANCELLED));
System.out.println(check2(OrderStatus.NEW));
System.out.println(check2(OrderStatus.SHIPPED));
System.out.println("----------------------------");
System.out.println(handleOrder(OrderStatus.NEW));
System.out.println(handleOrder(OrderStatus.PROCESSING));
System.out.println(handleOrder(OrderStatus.SHIPPED));

}

//Switch Pattern Matching (Java 17+/21 feature)
private static String check1(Object obj) {
return switch (obj) {
case Integer i -> "Integer: " + i;
case String s -> "String: " + s;
case null -> "Null value";
default -> "Unknown";
};
}

//Traditional Approach
private static String legacyApproach1(Object obj) {
if (obj instanceof String) {
String s = (String) obj;
return "String: " + s;
} else if (obj instanceof Integer) {
Integer i = (Integer) obj;
return "Integer: " + i;
} else return "Unknown";
}

//Pattern Matching using Enums
private static String check2(OrderStatus status) {
return switch (status) {
case NEW -> "Order placed";
case PROCESSING -> "Order is being processed";
case SHIPPED -> "Order shipped";
case DELIVERED -> "Order delivered";
case CANCELLED -> "Order cancelled";
};
}

//Switch statement with Enum
private static String legacyApproach2(OrderStatus status) {
switch (status) {
case NEW:
return "Order placed";
case PROCESSING:
return "Order is being processed";
case SHIPPED:
return "Order shipped";
case DELIVERED:
return "Order delivered";
case CANCELLED:
return "Order cancelled";
default:
return "Unknown status";
}
}

//Real-World Example (Better Practice)
public static String handleOrder(OrderStatus status) {
return switch (status) {
case NEW -> initOrder();
case PROCESSING -> processOrder();
case SHIPPED -> shipOrder();
case DELIVERED -> completeOrder();
case CANCELLED -> cancelOrder();
};
}

private static String cancelOrder() {
return "Order cancelled";
}

private static String completeOrder() {
return "Order delivered";
}

private static String shipOrder() {
return "Order shipped";
}

private static String processOrder() {
return "Order is being processed";
}

private static String initOrder() {
return "Order placed";
}

}

--

--

Syed Nazeer
Syed Nazeer

Written by Syed Nazeer

I'm a Java enthusiast, developing passion for technical content creation.