Case with Switch keyword in java

The switch-case in java is a flow control structure that tests value of a variable against a list of predefined values.

General syntax of switch-case is as follows:

  1. switch (expression) {
  2.  
  3.     case constant_1:
  4.  
  5.                         // code block for case 1
  6.  
  7.                        break;
  8.  
  9.     case constant_2:
  10.  
  11.                        // code block for case 2
  12.  
  13.                        break;
  14.  
  15.     case constant_3:
  16.  
  17.                        // code block for case 3
  18.  
  19.                        break;
  20.  
  21.     //...
  22.    //'''
  23.     case constant_n:
  24.  
  25.                       // code block for case n
  26.  
  27.                        break;
  28.  
  29.     default:
  30.                       // This code block executes if none of the above cases match
  31.        
  32. }
switch (expression) {

    case constant_1:

                        // code block for case 1

                       break;

    case constant_2:

                       // code block for case 2

                       break;

    case constant_3:

                       // code block for case 3

                       break;

    //...
   //'''
    case constant_n:

                      // code block for case n

                       break;

    default:
                      // This code block executes if none of the above cases match
       
}


Note:


The expression is a variable or an expression which must be evaluated to one of the following types:

Primitive numbers : byte,char and int.

Primitive wrappers : Byte, Short, Character and Integer.

Enumerated types.

String objects (allowed since Java 1.7)

The constant_1, constant_2, constant_3, …, constant_n should be a constant or literals whose data type is one of the allowed types listed above.

Each case is evaluated from top to bottom, until a case is matched and a break statement is found.

If a case matches the expression, the code block after the case clause will be executed, until a break statement is reached.

It is not mandatory to have a break statement in each case. If a matching case block does not have a break statement, the execution will fall through the next case block, until a first break statement is reached or end of switch statement is encountered.

The code block inside default case will be executed if there is no matching case found for the given expression.

Examples:

Ex 1: Switch case with integer number

  1. int number = 2;
  2.  
  3. switch (number) {
  4.  
  5.     case 1:
  6.                      System.out.println("I am from case 1 ");
  7.                      break;
  8.  
  9.     case 2:
  10.                      System.out.println("I am from case 2 ");
  11.                      break;
  12.  
  13.     case 3:
  14.                      System.out.println("I am from case 3 ");
  15.                      break;
  16.  
  17.     default:
  18.                      System.out.println("I am from default case ");
  19. }
  20.  
  21. System.out.println(" Done ");
int number = 2;

switch (number) {

    case 1:
                     System.out.println("I am from case 1 ");
                     break;

    case 2:
                     System.out.println("I am from case 2 ");
                     break;

    case 3:
                     System.out.println("I am from case 3 ");
                     break;

    default:
                     System.out.println("I am from default case ");
}

System.out.println(" Done ");



Since we are passing number 2 as an input to Switch case, it matches with case 2 and executes code block inside it.

Since we have break statement after that code block in Case 2 , it will stop the further cases to be evaluated and start the execution after the Case block.

Ex 2: Switch case with String(allowed only in java 7 and above)

  1. String month = "March";
  2.  
  3. int monthNumber = 0;
  4.  
  5.  switch (month.toLowerCase()) {
  6.  
  7.             case "january":
  8.                              monthNumber = 1;
  9.                              break;
  10.  
  11.             case "february":
  12.                              monthNumber = 2;
  13.                              break;
  14.  
  15.             case "march":
  16.                              monthNumber = 3;
  17.                              break;
  18.  
  19.             case "april":
  20.                              monthNumber = 4;
  21.                              break;
  22.  
  23.             case "may":
  24.                              monthNumber = 5;
  25.                              break;
  26.  
  27.             case "june":
  28.                              monthNumber = 6;
  29.                              break;
  30.  
  31.             case "july":
  32.                              monthNumber = 7;
  33.                              break;
  34.  
  35.             case "august":
  36.                              monthNumber = 8;
  37.                              break;
  38.  
  39.             case "september":
  40.                              monthNumber = 9;
  41.                              break;
  42.  
  43.             case "october":
  44.                              monthNumber = 10;
  45.                              break;
  46.  
  47.             case "november":
  48.                              monthNumber = 11;
  49.                              break;
  50.  
  51.             case "december":
  52.                              monthNumber = 12;
  53.                              break;
  54.  
  55.             default:
  56.                              monthNumber = 0;
  57.                              break;
  58.         }
  59.  
  60. System.out.println("Month number for "+month+" is "+monthNumber);
String month = "March";

int monthNumber = 0;

 switch (month.toLowerCase()) {

            case "january":
                             monthNumber = 1;
                             break;

            case "february":
                             monthNumber = 2;
                             break;

            case "march":
                             monthNumber = 3;
                             break;

            case "april":
                             monthNumber = 4;
                             break;

            case "may":
                             monthNumber = 5;
                             break;

            case "june":
                             monthNumber = 6;
                             break;

            case "july":
                             monthNumber = 7;
                             break;

            case "august":
                             monthNumber = 8;
                             break;

            case "september":
                             monthNumber = 9;
                             break;

            case "october":
                             monthNumber = 10;
                             break;

            case "november":
                             monthNumber = 11;
                             break;

            case "december":
                             monthNumber = 12;
                             break;

            default: 
                             monthNumber = 0;
                             break;
        }

System.out.println("Month number for "+month+" is "+monthNumber);


Ex 3: Switch case with Enum


First lets define the Enum containing possible status as below

  1. enum Status {
  2.  
  3.     CREATED,
  4.  
  5.     INPROGRESS,
  6.  
  7.     COMPLETED
  8.  
  9. };
  10.  
  11. String statusStr="";
  12.  
  13. Status status = Status.INPROGRESS;
  14.  
  15. switch (status) {
  16.  
  17.     case CREATED:
  18.                      statusStr="Task has been just created";
  19.                      break;
  20.  
  21.     case INPROGRESS:
  22.                      statusStr="Task is still in progress";
  23.                      break;
  24.  
  25.     case COMPLETED:
  26.                      statusStr="Task has completed just now";
  27.                      break;
  28.  
  29.      default:
  30.                      statusStr="Some problem with the task";
  31.                      break;
  32. }
  33.  
  34. System.out.println("The status of the task is -> " + statusStr);
enum Status {

    CREATED,

    INPROGRESS,

    COMPLETED

};

String statusStr="";

Status status = Status.INPROGRESS;

switch (status) {

    case CREATED:
                     statusStr="Task has been just created";
                     break;

    case INPROGRESS:
                     statusStr="Task is still in progress";
                     break;

    case COMPLETED:
                     statusStr="Task has completed just now";
                     break;

     default:
                     statusStr="Some problem with the task";
                     break;
}

System.out.println("The status of the task is -> " + statusStr);


About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on