Fundamentals24 / 25
Up next

Switch expression

class Main {
    static String classify(int n) {
        // arrow labels never fall through, and this yields a value
        return switch (Integer.signum(n)) {
            case -1 -> "negative";
            case 0 -> "zero";
            default -> "positive";
        };
    }

    public static void main(String[] args) {
        System.out.println(classify(-5) + " " + classify(0) + " " + classify(9));
    }
}
What you're solving

arrow-form switch returns a value