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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
       
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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 ");
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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);
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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);
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

Karibasappa G C (KB)
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