Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java switch Keyword
Usage and Examples



switch Keyword in Java

The switch keyword in Java is used for decision-making. It's an alternative to writing many if-else-if statements, and it allows you to test a single expression against multiple possible values. When readability and clarity matter, switch often makes your code cleaner and more elegant.

Basic Syntax of switch

switch (expression) {
    case value1:
        // code block
        break;
    case value2:
        // code block
        break;
    ...
    default:
        // default block
}

Here’s what each part means:

Example 1: Integer-Based switch

int day = 3;
switch (day) {
    case 1:
        System.out.println("Monday");
        break;
    case 2:
        System.out.println("Tuesday");
        break;
    case 3:
        System.out.println("Wednesday");
        break;
    default:
        System.out.println("Another day");
}
Wednesday

Since day is 3, the third case matches and prints "Wednesday". The break prevents the next cases from being executed.

Example 2: What Happens Without break?

int rating = 4;
switch (rating) {
    case 5:
        System.out.println("Excellent");
    case 4:
        System.out.println("Good");
    case 3:
        System.out.println("Fair");
    default:
        System.out.println("Needs improvement");
}
Good
Fair
Needs improvement

Without break, the switch statement "falls through" and executes all subsequent cases. That’s why it prints everything starting from case 4.

Example 3: Using String in switch (Java 7+)

String role = "admin";
switch (role) {
    case "admin":
        System.out.println("Welcome Admin");
        break;
    case "editor":
        System.out.println("Welcome Editor");
        break;
    default:
        System.out.println("Role not recognized");
}
Welcome Admin

Java allows String types in switch statements starting with Java 7, making it useful for text-based conditions.

Example 4: switch with char

char grade = 'B';
switch (grade) {
    case 'A':
        System.out.println("Excellent!");
        break;
    case 'B':
        System.out.println("Well done");
        break;
    case 'C':
        System.out.println("You passed");
        break;
    default:
        System.out.println("Invalid grade");
}
Well done

Example 5: switch with enum

enum Direction {
    NORTH, SOUTH, EAST, WEST
}

Direction dir = Direction.EAST;
switch (dir) {
    case NORTH:
        System.out.println("You are heading North");
        break;
    case EAST:
        System.out.println("You are heading East");
        break;
}
You are heading East

enum types make switch more expressive and type-safe, often used in game dev or control systems.

Best Practices for switch

When Not to Use switch

If you need to compare ranges or perform more complex conditions, if-else-if blocks may be more appropriate.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M