Java 17: Switch expressions. Compare if-else, classic switch, map lookups, switch expressions and enums.
Problem
Map an HTTP status code to a retry policy (RETRY, RETRY_AFTER, FAIL, OK), and decide whether a DayOfWeek is a weekend. Typical refactoring questions about modern Java control flow.
Approach 1: if-else chain (feasible)
Readable for a few cases, but long chains hide gaps and overlaps.
class RetryIfElse {
static String policy(int status) {
if (status >= 200 && status < 300) return "OK";
if (status == 429 || status == 503) return "RETRY_AFTER";
if (status == 500 || status == 502 || status == 504) return "RETRY";
return "FAIL";
}
}Approach 2: Classic switch statement (Java 7)
Needs break in every case; forgetting one causes fall-through bugs. It is a statement, so you must assign a variable in each branch.
class RetryClassicSwitch {
static String policy(int status) {
String result;
switch (status) {
case 429:
case 503:
result = "RETRY_AFTER";
break;
case 500:
case 502:
case 504:
result = "RETRY";
break;
default:
result = status / 100 == 2 ? "OK" : "FAIL";
}
return result;
}
}Approach 3: Map lookup (data instead of code)
Good when mappings come from configuration. Lookup is O(1), but ranges (all 2xx) need special handling.
import java.util.Map;
class RetryMap {
private static final Map<Integer, String> SPECIAL = Map.of(429, "RETRY_AFTER", 503, "RETRY_AFTER",
500, "RETRY", 502, "RETRY", 504, "RETRY");
static String policy(int status) {
return SPECIAL.getOrDefault(status, status / 100 == 2 ? "OK" : "FAIL");
}
}Approach 4: Switch expression (Java 14+, in Java 17 LTS)
Arrow labels never fall through, several labels share one case, the switch returns a value, and yield returns from a block. For enums, the compiler checks that a switch expression covers every constant.
import java.time.DayOfWeek;
class RetrySwitchExpression {
static String policy(int status) {
return switch (status) {
case 429, 503 -> "RETRY_AFTER";
case 500, 502, 504 -> "RETRY";
default -> {
int family = status / 100;
yield family == 2 ? "OK" : "FAIL";
}
};
}
static boolean weekend(DayOfWeek day) {
return switch (day) { // exhaustive over the enum: no default needed
case SATURDAY, SUNDAY -> true;
case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> false;
};
}
}Approach 5: Enum with data (optimal for extensibility)
When each status carries several properties (retryable, backoff), put them on an enum and look up once. Behaviour lives with the data and adding a status is one line.
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
enum HttpPolicy {
TOO_MANY_REQUESTS(429, "RETRY_AFTER"), SERVICE_UNAVAILABLE(503, "RETRY_AFTER"),
INTERNAL_ERROR(500, "RETRY"), BAD_GATEWAY(502, "RETRY"), GATEWAY_TIMEOUT(504, "RETRY");
final int code;
final String policy;
HttpPolicy(int code, String policy) { this.code = code; this.policy = policy; }
private static final Map<Integer, HttpPolicy> BY_CODE =
Arrays.stream(values()).collect(Collectors.toMap(p -> p.code, Function.identity()));
static String policyFor(int status) {
HttpPolicy p = BY_CODE.get(status);
return p != null ? p.policy : status / 100 == 2 ? "OK" : "FAIL";
}
}
class RetryDemo {
public static void main(String[] args) {
for (int s : new int[] {200, 204, 429, 500, 404, 503}) {
System.out.println(s + " " + RetryIfElse.policy(s) + " " + RetryClassicSwitch.policy(s) + " "
+ RetryMap.policy(s) + " " + RetrySwitchExpression.policy(s) + " " + HttpPolicy.policyFor(s));
}
System.out.println(RetrySwitchExpression.weekend(java.time.DayOfWeek.SUNDAY)); // true
}
}Feasible → optimal
All are O(1) in practice (the JVM compiles dense switches to jump tables). If-else and classic switch are feasible; the switch expression is the modern default for control flow; a map or enum is optimal when the mapping is data that grows or comes from configuration.
Follow-up questions interviewers ask
Try answering each one before you open it.
What is the difference between a switch statement and a switch expression with arrows?
A switch expression produces a value and must be exhaustive (every possible input handled, or a default). Arrow labels (->) work in both statements and expressions and never fall through; each case is a single expression, a block with yield, or a throw. Old colon labels can still be used in a switch expression, but then you need yield and fall-through is possible.
Can you switch on strings, and how does it work internally?
Yes, since Java 7. The compiler switches on hashCode() first, then confirms with equals() to handle collisions, so it is efficient. A null selector throws NullPointerException in classic switches; Java 21's pattern matching switch adds an explicit case null.