java use variable in switch code example

Example 1: switch statement in java

// switch case in java example programs
public class SwitchStatementExample
{
   public static void main(String[] args)
   {
      char grade = 'A';
      switch(grade) {
          case 'A' :
              System.out.println("Distinction.");
              break;
          case 'B' :
          case 'C' :
              System.out.println("First class.");
              break;
          case 'D' :
              System.out.println("You have passed.");
          case 'F' :
              System.out.println("Fail. Try again.");
              break;
          default :
              System.out.println("Invalid grade");
      }
      System.out.println("Grade is: " + grade);
   }
}

Example 2: java switch on what classes

A switch works with the byte, short, char, and int primitive data types.
It also works with enumerated types (discussed in Enum Types), the String
class, and a few special classes that wrap certain primitive types: 
Character , Byte , Short , and Integer (discussed in Numbers and Strings).