switch Statement

noun

say it: “switch STAYT-ment” /swɪtʃ ˈsteɪtmənt/

Code that picks one of several paths by matching a single value against a list of cases.

What does switch Statement do in Java?

switch Statement — A statement that selects a code path by matching one value against cases.

class Shirt {
    String sizeName(String size) {
        return switch (size) {
            case "M" -> "Medium";
            default -> "Other";
        };
    }
}

In a sentence

  • “I used a switch statement on the menu choice, with one case for each option from 1 to 4.”

Level: Novice · More Java terms at this level

Updated

In other subjects