0
0
JavaHow-ToBeginner · 3 min read

Switch Case with String in Java: Syntax and Example

In Java, you can use switch statements with String values starting from Java 7. The switch compares the string expression with each case label and executes the matching block.
📐

Syntax

The switch statement evaluates a String expression and compares it with each case label. If a match is found, the corresponding block runs. The default block runs if no cases match.

  • switch(expression): The string to check.
  • case "value": A string to compare with the expression.
  • break; Stops checking further cases.
  • default: Runs if no case matches.
java
switch (stringVariable) {
    case "value1":
        // code block
        break;
    case "value2":
        // code block
        break;
    default:
        // default code block
        break;
}
💻

Example

This example shows how to use a switch statement with strings to print messages based on the day of the week.

java
public class SwitchStringExample {
    public static void main(String[] args) {
        String day = "Tuesday";

        switch (day) {
            case "Monday":
                System.out.println("Start of the work week.");
                break;
            case "Tuesday":
                System.out.println("Second day of the work week.");
                break;
            case "Friday":
                System.out.println("Last workday before weekend!");
                break;
            default:
                System.out.println("Midweek days or weekend.");
                break;
        }
    }
}
Output
Second day of the work week.
⚠️

Common Pitfalls

Common mistakes when using switch with strings include:

  • Forgetting break; causes fall-through to next case.
  • Using null as the switch expression causes NullPointerException.
  • Case labels must be constant strings, not variables.

Always ensure the switch expression is not null before switching.

java
public class SwitchNullExample {
    public static void main(String[] args) {
        String input = null;

        // This will throw NullPointerException
        // switch (input) {
        //     case "test":
        //         System.out.println("Test case");
        //         break;
        // }

        // Correct way: check for null first
        if (input != null) {
            switch (input) {
                case "test":
                    System.out.println("Test case");
                    break;
                default:
                    System.out.println("Default case");
                    break;
            }
        } else {
            System.out.println("Input is null, cannot switch.");
        }
    }
}
Output
Input is null, cannot switch.
📊

Quick Reference

Remember these tips when using switch with strings in Java:

  • Supported since Java 7.
  • Case labels must be constant strings.
  • Always include break; to avoid fall-through.
  • Check for null before switching.
  • default handles unmatched cases.

Key Takeaways

Java supports switch statements with strings starting from Java 7.
Always use break statements to prevent fall-through between cases.
Never switch on a null string to avoid NullPointerException.
Case labels must be constant string literals, not variables.
Use the default case to handle unmatched string values.