What are alternatives to switch expressions and how do they compare?

alternatives to switch expressions, Java switch alternatives, if-else statements, pattern matching in Java
This article explores alternatives to switch expressions in Java, comparing them with if-else statements and pattern matching.

        // Using if-else statements as an alternative to switch expressions
        String day = "MONDAY";
        String dayType;

        if ("MONDAY".equals(day) || "TUESDAY".equals(day) || "WEDNESDAY".equals(day)
            || "THURSDAY".equals(day) || "FRIDAY".equals(day)) {
            dayType = "Weekday";
        } else if ("SATURDAY".equals(day) || "SUNDAY".equals(day)) {
            dayType = "Weekend";
        } else {
            dayType = "Invalid day";
        }

        System.out.println(dayType);

        // Using pattern matching (Java 16 and above) as an alternative to switch expressions
        Object obj = "Hello";
        switch (obj) {
            case String s -> System.out.println("It's a string: " + s);
            case Integer i -> System.out.println("It's an integer: " + i);
            default -> System.out.println("Unknown type");
        }
    

alternatives to switch expressions Java switch alternatives if-else statements pattern matching in Java