Java Switch Statement in Detail

java

Java switch statement executes one statement out of multiple conditions. Java switch statement is similar to if-else if ladder.

The switch statement supports multiple data types in the condition like byte, short, int, long, enumerations, some wrapper types like Byte, Short, Integer, Long. Since, the release of Java 7, the use of Strings is also supported in the switch conditions.

Points to remember about a Switch Statement

  • Switch statement works by comparing the equality of a variable against multiple values.

  • You can have N number of case statements within a switch. Each case is followed by the value to be compared to and a colon (‘:’).

  • The value for a case must be the same data type as the variable in the switch and it must be a constant or a literal. It doesn’t allow variables.

  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.

  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.

  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Syntax

1
2
3
4
5
6
7
8
9
10
11
switch(expression){
	case value1:
		//code to be executed;
		break;
	case value2:
		//code to be executed;
		break;
	……..
	default:
		//code to be executed when all cases are unmatched;
}

Switch statement

Examples

Finding the month

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
public class example {
 public static void main(String[] args) {
  int month = 7;
  String monthString = "";
  switch (month) {
   case 1:
    monthString = "1 - January";
    break;
   case 2:
    monthString = "2 - February";
    break;
   case 3:
    monthString = "3 - March";
    break;
   case 4:
    monthString = "4 - April";
    break;
   case 5:
    monthString = "5 - May";
    break;
   case 6:
    monthString = "6 - June";
    break;
   case 7:
    monthString = "7 - July";
    break;
   case 8:
    monthString = "8 - August";
    break;
   case 9:
    monthString = "9 - September";
    break;
   case 10:
    monthString = "10 - October";
    break;
   case 11:
    monthString = "11 - November";
    break;
   case 12:
    monthString = "12 - December";
    break;
   default:
    System.out.println("Invalid Month!");
  }
  System.out.println(monthString);
 }
}

Switch with fall-through

The case will be executed until a break statement is encountered.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public class example {
 public static void main(String[] args) {
  int number = 20;
  switch (number) {
   case 10:
    System.out.println("10");
   case 20:
    System.out.println("20");
   case 30:
    System.out.println("30");
   default:
    System.out.println("Not in 10, 20 or 30");
  }
 }
}

Switch with String expression since Java SE 7

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class example {
 public static void main(String[] args) {
  String levelString = "Expert";
  int level = 0;
  switch (levelString) {
   case "Beginner":
    level = 1;
    break;
   case "Intermediate":
    level = 2;
    break;
   case "Expert":
    level = 3;
    break;
   default:
    level = 0;
    break;
  }
  System.out.println("Your Level is: " + level);
 }
}

Nested Switch Statement

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
public class example {
 public static void main(String args[]) {
  char branch = 'M';
  int collegeYear = 3;
  switch (collegeYear) {
   case 1:
    System.out.println("English, Maths, Science");
    break;
   case 2:
    switch (branch) {
     case 'C':
      System.out.println("Operating System, Java, Data Structure");
      break;
     case 'E':
      System.out.println("Micro processors, Logic switching theory");
      break;
     case 'M':
      System.out.println("Drawing, Manufacturing Machines");
      break;
    }
    break;
   case 3:
    switch (branch) {
     case 'C':
      System.out.println("Computer Organization, MultiMedia");
      break;
     case 'E':
      System.out.println("Fundamentals of Logic Design, Microelectronics");
      break;
     case 'M':
      System.out.println("Internal Combustion Engines, Mechanical Vibration");
      break;
    }
    break;
   case 4:
    switch (branch) {
     case 'C':
      System.out.println("Data Communication and Networks, MultiMedia");
      break;
     case 'E':
      System.out.println("Embedded System, Image Processing");
      break;
     case 'M':
      System.out.println("Production Technology, Thermal Engineering");
      break;
    }
    break;
  }
 }
}

Switch statement with Enumeration

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
public class JavaSwitchEnumExample {
 public enum Month {
  Jan,
  Feb,
  Mar,
  Apr,
  May,
  Jun,
  Jul,
  Aug,
  Sept,
  Oct,
  Nov,
  Dec
 }
 public static void main(String args[]) {
  Month[] monthNow = Month.values();
  for (Month Now: monthNow) {
   switch (Now) {
    case Jan:
     System.out.println("January");
     break;
    case Feb:
     System.out.println("February");
     break;
    case Mar:
     System.out.println("March");
     break;
    case Apr:
     System.out.println("April");
     break;
    case May:
     System.out.println("May");
     break;
    case Jun:
     System.out.println("June");
     break;
    case Jul:
     System.out.println("July");
     break;
    case Aug:
     System.out.println("August");
     break;
    case Sept:
     System.out.println("September");
     break;
    case Oct:
     System.out.println("October");
     break;
    case Nov:
     System.out.println("November");
     break;
    case Dec:
     System.out.println("December");
     break;
   }
  }
 }
}

Switch with wrapper classes

Java switch statement uses Byte, Short, Integer and Long wrapper classes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class example {
 public static void main(String args[]) {
  Integer number = 35;
  switch (number) {
   case (16):
    System.out.println("You are under 18.");
    break;
   case (18):
    System.out.println("You are eligible for vote.");
    break;
   case (65):
    System.out.println("You are senior citizen.");
    break;
   default:
    System.out.println("Please give the valid age.");
    break;
  }
 }
}

If you liked this article, please upvote and recommend it to show your support. Feel free to ask any questions in the comments below.


We publish articles on web development and technology frequently. Consider subscribing to our newsletter or follow us on our social channels (twitter, Facebook, LinkedIn).

Share and support us

Share on social media and help us reach more people