Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Switch

When one value decides between many different actions, a long chain of else if from If ... Else becomes hard to read. A switch compares one value against a list of cases and runs the matching one. It works with whole numbers, characters, strings and enums.

The arrow form

Modern Java (14 and later) has a clean arrow syntax. Each case has a label, an arrow and what to do:

public class Main {
    public static void main(String[] args) {
        int day = 3;
        switch (day) {
            case 1 -> System.out.println("Monday");
            case 2 -> System.out.println("Tuesday");
            case 3 -> System.out.println("Wednesday");
            case 4 -> System.out.println("Thursday");
            case 5 -> System.out.println("Friday");
            case 6, 7 -> System.out.println("Weekend");
            default -> System.out.println("Not a day");
        }
    }
}
Output
Wednesday
  • case 6, 7 handles two values in one case.
  • default runs when no case matches. It is optional, but it is good practice to include it.
  • With arrows, only the matching case runs; there is no falling through.

Switch expressions

A switch can also produce a value, which you store in a variable. Every possible value must be covered, so a default is usually needed:

public class Main {
    public static void main(String[] args) {
        String fruit = "mango";
        int price = switch (fruit) {
            case "apple" -> 120;
            case "banana" -> 40;
            case "mango" -> 150;
            default -> 0;
        };
        System.out.println(fruit + " costs " + price + " per kg");
    }
}
Output
mango costs 150 per kg

Notice the semicolon after the closing bracket: the whole switch is part of one assignment statement. When a case needs several statements, give it a block and use yield to return the value:

public class Main {
    public static void main(String[] args) {
        char op = '*';
        int a = 6, b = 7;
        int result = switch (op) {
            case '+' -> a + b;
            case '-' -> a - b;
            case '*' -> {
                System.out.println("Multiplying...");
                yield a * b;
            }
            default -> throw new IllegalArgumentException("Unknown operator");
        };
        System.out.println(result);
    }
}
Output
Multiplying...
42

The classic form with break

Older code uses colons and break. Without break, execution falls through into the next case, which is a common source of bugs:

public class Main {
    public static void main(String[] args) {
        int level = 2;
        switch (level) {
            case 1:
                System.out.println("Easy");
                break;
            case 2:
                System.out.println("Medium");
                // no break: falls through into case 3!
            case 3:
                System.out.println("Hard");
                break;
            default:
                System.out.println("Unknown");
        }
    }
}
Output
Medium
Hard

For level 2 it prints both "Medium" and "Hard". Prefer the arrow form in new code. You will see switch again with enums in the Enums topic.

Practice exercises

  1. Store a month number (1 to 12) and print the number of days in that month with a switch expression.
  2. Write a small calculator: two numbers and an operator character, using a switch expression.
  3. Print "vowel" or "consonant" for a letter, using case 'a', 'e', 'i', 'o', 'u'.