Switch Case In Java

In this page, we will learn about Switch Case In Java, What is Switch Case In Java?, Switch Case Syntax in Java, What is Switch Case In Java?, Switch Case Example with int in Java, Switch Case Example with char in Java, Switch Case is fall-through In Java, Nested switch case systax in Java, Nested Switch Case Example In Java.


What is Switch Case In Java?

The Java switch case statement conducts one statement from multiplex agreements. It is like if-else-if ladder assertion. The switch case statement pursuits with byte, short, int, long, enum types, String and few wrapper types like Byte, Short, Int, and Long. Since Java 7, you’ll be able to use strings in the switch statement.

In another words, the switch case statement tests the equilibrium of a variable opposed to multiple values.

Points to keep in mind:

  • There be able one or N number of case values for a switch manifestation.
  • The case value need to be switch manifestation type only. The case value must be verbal or constant. It doesn't allow changeable.
  • The case values must be unique. In case of similar value, it provides compile-time error.
  • The Java switch manifestation must be of byte, short, int, long (with its Wrapper type), enums and string.
  • Every case statement be able to break statement which is optional. When control touches to the break statement, it jumps the control after the switch manifestation. If a break statement is not found, it performs the next case.
  • The case value can have a default label which is elective.

Switch Case Syntax in Java :-


  switch(expression){    
    case value1:    
        //code to be executed;    
        break;  //optional  
    case value2:    
        //code to be executed;    
        break;  //optional	    
    default:  
        //if all cases are not matched;   
    }           

Switch CaseIn java

Switch Case Example with int in Java :-


  import java.io.*;
  public class Example {    
    public static void main(String[] args) {     
       int month = 2;    
         
       switch(month){     
         case 1: System.out.println("First Month");   
                 break;    
         case 2: System.out.println("Second Month");   
                 break;    
         default:System.out.println("Invalid Month!");    
       }    
    
    }    
  }   

Output:


   Second Month


Switch Case Example with char in Java :-


  import java.io.*;
  public class Example {    
    public static void main(String[] args) {     
       char letter = 'a';    
         
       switch(letter){     
         case 'a': System.out.println("This is a");   
                   break;    
         case 'b': System.out.println("This is b");   
                   break;    
         default:System.out.println("Invalid!");    
       }    
    
    }    
  }   

Output:


    This is a


Switch Case is fall-through In Java :-

The Java switch statement is fall-through. It suggests that it executes all statements after the primary match when a break statement is not at hand.

Example :-


 import java.io.*;
 public class Main {
   public static void main(String args[]){
    int age = 1;
    switch(age){     
        case 1: System.out.println("1 Year");   
                break;    
        case 2: System.out.println("2 Years");   
                break;    
        default:System.out.println("Invalid Age!");    
      }  
   }
 }

Output:


   1 Year
   2 Years
   Invalid Age!


Nested switch case systax in Java :-


  switch(expression){    
    case value1:    
        //code to be executed;    
        break; 
    case value2:    
        //Nested Switch Case  
        switch(expression2){
            case value1:    
            //code to be executed;    
            break;  //optional
            default:  
            //if all cases are not matched;   
        }

        
        break;  //optional	    
        default:  
            //if all cases are not matched;   
  }     

Nested Switch Case Example In Java:-


 import java.io.*;
 public class Main {
   public static void main(String args[]){
    int age = 1;
    int month = 2;
    switch(age){     
      case 1: switch(month){
          case 1: 
              System.out.println("Age 1, Month 1");   
              break;
          case 2: 
              System.out.println("Age 1, Month 2");   
              break;
          default:
              System.out.println("Invalid age,Month!");
        }  
        break;    
        case 2: System.out.println("2 Years");   
                break;    
        default:System.out.println("Invalid Age!");    
      }
   }
 }

Output:


  Age 1, Month 2