Skip to content

Latest commit

 

History

History
25 lines (23 loc) · 615 Bytes

combining_cases.md

File metadata and controls

25 lines (23 loc) · 615 Bytes

Combining Cases

If you have multiple constant values that should be handled in the same way, you can combine case labels by separating their values with a comma.

String scientificName(String vegetable) {
    switch (vegetable) {
        case "apple" -> {
            return "Malus pumila";
        }
        case "cabbage", "brussel sprouts", "kale", "cauliflower" -> {
            // Look it up. Kinda wild.
            return "Brassica oleracea";
        }
        default -> {
            return "unknown";
        }
    }
}
~
~void main() {
~    System.out.println(scientificName("cabbage"));
~}