Sitelet https://java2blog.com/switch-case-java/
[wp-night-mode-button]

Switch case in java

Switch case in java is alternative to if else if ladder. It is used to execute statements based on some conditions.
[toc]

Syntax of Switch case in java

switch(expression) {
   case value_1 :
      // Statements
      break; // optional

   case value_2 :
      // Statements
      break; // optional

   // Default is executed when the expression does not match with any of the above conditions.
   default : // Optional
      // Statements
  }
}

Let’s understand it with the help of simple example.
We are going to print weekday based on integer. 0 represents Sunday, 1 represents Monday and so on..

package org.arpit.java2blog;

public class SwitchCaseExample {

    public static void main(String[] args) {
        int dayOfWeek=5;
        switch(dayOfWeek) {
           case 0 :
              System.out.println("Sunday");
              break; 
           case 1 :
                  System.out.println("Monday");
                  break;
           case 2 :
                  System.out.println("Tuesday");
                  break;
           case 3 :
                  System.out.println("Wednesday");
                  break;
           case 4 :
                  System.out.println("Thrusday");
                  break;
           case 5 :
                  System.out.println("Friday");
                  break;
           case 6 :
                  System.out.println("Saturday");
                  break;          

           // Default is executed when expression does not match with any of above conditions.
           default : // Optional
             System.out.println("Invalid day of week");
        }       
    }
}

Output:

Friday

As we have highlighted in syntax, break keyword is optional.
Let’s see what happens if we do not use break statement.

package org.arpit.java2blog;

public class SwitchCaseExample {

    public static void main(String[] args) {
        int dayOfWeek=5;
        switch(dayOfWeek) {
           case 0 :
                  System.out.println("Sunday");
           case 1 :
                  System.out.println("Monday");
           case 2 :
                  System.out.println("Tuesday");
           case 3 :
                  System.out.println("Wednesday");
           case 4 :
                  System.out.println("Thrusday");
           case 5 :
                  System.out.println("Friday");
           case 6 :
                  System.out.println("Saturday");

           // Default is executed when expression does not match with any of above conditions.
           default : // Optional
             System.out.println("Invalid day of week");
        }       
    }
}

Output:

Friday
Saturday
Invalid day of week

As you can see here, if you do not use break statement, it will execute all the statements once condition is met.

Switch case String example

You can use String also in expression from Java 7 onwards.
Let’s understand with help of simple example:

package org.arpit.java2blog;

public class SwitchCaseExample {

    public static void main(String[] args) {
        int dayOfWeek=5;
        switch(dayOfWeek) {
          case 0 :
              System.out.println("Sunday");
              break;
           case 1 :
                  System.out.println("Monday");
                  break;
           case 2 :
                  System.out.println("Tuesday");
                  break;
           case 3 :
                  System.out.println("Wednesday");
                  break;
           case 4 :
                  System.out.println("Thrusday");
                  break;
           case 5 :
                  System.out.println("Friday");
                  break;
           case 6 :
                  System.out.println("Saturday");
                  break;          

           // Default is executed when expression does not match with any of above conditions.
           default : // Optional
             System.out.println("Invalid day of week");
        }       
    }
}

Output:

Friday

Switch case uses equals method comparison internally, so case statement is case sensitive here.

Choosing between Switch case and if-else if it depends on readability and various factors. You can choose as per your needs.

that’s all about switch case in java.

[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.