DEV Community

Cover image for Day 7: Understanding Java `switch` Case Statements
Karthick Narayanan
Karthick Narayanan

Posted on

Day 7: Understanding Java `switch` Case Statements

What is a switch Statement?

A switch statement is used to execute one block of code based on the value of an expression.

Instead of checking multiple conditions using if–else, we can use switch when:

  • We compare one value
  • Against multiple fixed values

Data Types Supported in switch

switch supports:

  • byte, short, int, char
  • String

(Other types like long, float, double, and boolean are not used here.)


=> Usage of break in switch

break is used to stop the execution of a switch case once a matching case is found.

  • Without break, Java will continue executing the next cases (called fall-through)
  • With break, Java exits the switch block immediately

In simple words:

break tells Java: “Stop here and come out of the switch.”

Traditional switch Case (Using break)

This is the basic and commonly used form of switch.

=> Example:

String mark = "Karthick";

switch (mark) { // Expression
    case "abc":
        System.out.println("A grade");
        break;
    case "xyz":
        System.out.println("B grade");
        break;
    case "asd":
        System.out.println("C grade");
        break;
    case "klo":
        System.out.println("D grade");
        break;
    default:
        System.out.println("Fail");
}
Enter fullscreen mode Exit fullscreen mode

=> Explanation:

  • The switch checks the value of mark
  • If it matches a case, that block runs
  • break stops execution from falling into the next case
  • default runs when no case matches

=> Usage of default in switch

default is executed when none of the case values match the switch expression.

  • It acts like an else condition
  • It is optional but recommended

In simple words:

default handles unexpected or unmatched values.


=> Enhanced switch Case (Arrow -> Style)

Java also provides a simpler and cleaner syntax using the arrow (->).

=> Example:

String mark = "Karthick";

switch (mark) {
    case "abc" -> System.out.println("A grade");
    case "xyz" -> System.out.println("B grade");
    case "asd" -> System.out.println("C grade");
    case "klo" -> System.out.println("D grade");
    default -> System.out.println("Fail");
}
Enter fullscreen mode Exit fullscreen mode

=> Benefits:

  • No need to write break
  • Code is shorter and more readable
  • Reduces mistakes

=> switch as an Expression (Returning a Value)

In modern Java, switch can also return a value and store it in a variable.

=> Example:

String result = switch (mark) {
    case "abc" -> "A grade";
    case "xyz" -> "B grade";
    case "asd" -> "C grade";
    case "klo" -> "D grade";
    default -> "Fail";
};
Enter fullscreen mode Exit fullscreen mode

Here:

  • The switch returns a value
  • The result is stored in the variable result
  • This makes the code more compact and clean

When to Use switch

Use switch when:

  • You compare one variable
  • With multiple known values
  • You want better readability than long if–else chains

➡️ When Was switch Introduced in Java?

  • The switch statement was introduced in Java 1.0
  • Year: 1996

At that time, switch supported only:

  • byte
  • short
  • int
  • char

➡️ When Did Java Allow String in switch?

  • String was allowed inside switch from Java 7
  • Year: 2011

From Java 7 onwards, we can write:

switch (name) {
    case "abc":
        System.out.println("A grade");
}
Enter fullscreen mode Exit fullscreen mode

➡️ When was Arrow introduced in Java?

  • Enhanced switch with -> (arrow syntax) came in Java 14
  • It removed the need for break and made code cleaner

📝 Short-Note

  • break → exits the switch
  • default → runs when no case matches
  • switch introduced → Java 1.0 (1996)
  • String in switch → Java 7 (2011)

Top comments (0)