Explain about switch statement with an example.


 

Java Switch Statement

The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement.

In other words, the switch statement tests the equality of a variable against multiple values.

Points to Remember

  • There can be one or N number of case values for a switch expression.
  • The case value must be of switch expression type only. The case value must be literal or constant. It doesn't allow variables.
  • The case values must be unique. In case of duplicate value, it renders compile-time error.
  • The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
  • Each case statement can have a break statement which is optional. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
  • The case value can have a default label which is optional.

Syntax:

  1. switch(expression){    
  2. case value1:    
  3.  //code to be executed;    
  4.  break;  //optional  
  5. case value2:    
  6.  //code to be executed;    
  7.  break;  //optional  
  8. ......    
  9.     
  10. default:     
  11.  code to be executed if all cases are not matched;    
  12. }    
flow of switch statement in java

Example:

Finding Month Example:

  1. //Java Program to demonstrate the example of Switch statement  
  2. //where we are printing month name for the given number  
  3. public class SwitchMonthExample {    
  4. public static void main(String[] args) {    
  5.     //Specifying month number  
  6.     int month=7;    
  7.     String monthString="";  
  8.     //Switch statement  
  9.     switch(month){    
  10.     //case statements within the switch block  
  11.     case 1: monthString="1 - January";  
  12.     break;    
  13.     case 2: monthString="2 - February";  
  14.     break;    
  15.     case 3: monthString="3 - March";  
  16.     break;    
  17.     case 4: monthString="4 - April";  
  18.     break;    
  19.     case 5: monthString="5 - May";  
  20.     break;    
  21.     case 6: monthString="6 - June";  
  22.     break;    
  23.     case 7: monthString="7 - July";  
  24.     break;    
  25.     case 8: monthString="8 - August";  
  26.     break;    
  27.     case 9: monthString="9 - September";  
  28.     break;    
  29.     case 10: monthString="10 - October";  
  30.     break;    
  31.     case 11: monthString="11 - November";  
  32.     break;    
  33.     case 12: monthString="12 - December";  
  34.     break;    
  35.     default:System.out.println("Invalid Month!");    
  36.     }    
  37.     //Printing month of the given number  
  38.     System.out.println(monthString);  
  39. }    
  40. }   

Test it Now

Output:

7 - July