Java switch with multiple cases

Let’s learn java switch with multiple cases.

Java switch with multiple cases

In the rearest case we need to perform on multiple cases in switch statement. To do that use java switch with multiple case values. Let’s see an example on java switch for multiple cases.

public class SwitchMultipleCase
{
   public static void main(String[] args)
   {
      System.out.println(multipleCase("one"));
      System.out.println(multipleCase("two"));
   }
   static String multipleCase(final String number)
   {
      String str = null;
      switch(number)
      {
         case("one"):
         case("three"):
         case("five"):
             str = "odd numbers";
         break;

         case("two"):
         case("four"):
         case("six"):
             str = "even numbers";
         break;
         default:
             str = "please enter valid input";
      }
      return str;
   }
}


Output:

odd numbers
even numbers


Also read – HashSet in java