Code
public class SwitchExpressionUtils { // Basic switch expression public static String getDayType(DayOfWeek day) { return switch (day) { case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday"; case SATURDAY, SUNDAY -> "Weekend"; }; } // Switch expression with multiple statements public static int calculate(int value, String operation) { return switch (operation) { case "double" -> value * 2; case "square" -> value * value; case "triple" -> value * 3; default -> value; }; } // Switch expression with block public static String process(int value) { return switch (value) { case 0 -> "Zero"; case 1, 2, 3 -> "Small"; case 4, 5, 6 -> { String result = "Medium: " + value; yield result; // Use yield for blocks } default -> "Large"; }; } // Switch expression with enum public static int getPriority(Status status) { return switch (status) { case PENDING -> 1; case IN_PROGRESS -> 2; case COMPLETED -> 3; case CANCELLED -> 0; }; } // Switch expression with null handling public static String handleValue(String value) { return switch (value) { case null -> "Null value"; case "" -> "Empty string"; case String s when s.length() > 10 -> "Long string"; default -> "Normal string: " + value; }; } // Switch expression with pattern matching (Java 17+) public static String describe(Object obj) { return switch (obj) { case String s -> "String: " + s; case Integer i -> "Integer: " + i; case Double d -> "Double: " + d; case null -> "Null"; default -> "Unknown"; }; }}// Basic switch expressionDayOfWeek day = DayOfWeek.MONDAY;String dayType = switch (day) { case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday"; case SATURDAY, SUNDAY -> "Weekend";};// Switch expression with return valueint result = switch (operation) { case "add" -> a + b; case "subtract" -> a - b; case "multiply" -> a * b; case "divide" -> b != 0 ? a / b : 0; default -> 0;};// Switch expression with block and yieldString message = switch (status) { case "success" -> "Operation completed"; case "error" -> { String errorMsg = "Error occurred"; yield errorMsg; // Must use yield in blocks } case "pending" -> "Operation in progress"; default -> "Unknown status";};// Switch expression with enumenum Status { PENDING, IN_PROGRESS, COMPLETED }int priority = switch (status) { case PENDING -> 1; case IN_PROGRESS -> 2; case COMPLETED -> 3;};// Switch expression with null handling (Java 17+)String result2 = switch (value) { case null -> "Null value"; case "" -> "Empty"; case String s -> "String: " + s;};// Switch expression with pattern matching (Java 17+)String description = switch (obj) { case String s -> "String: " + s; case Integer i -> "Integer: " + i; case Double d -> "Double: " + d; case null -> "Null"; default -> "Unknown type";};// Switch expression for month daysint days = switch (month) { case JANUARY, MARCH, MAY, JULY, AUGUST, OCTOBER, DECEMBER -> 31; case APRIL, JUNE, SEPTEMBER, NOVEMBER -> 30; case FEBRUARY -> isLeapYear ? 29 : 28;};
Comments
No comments yet. Be the first!
Please login to leave a comment.