Switch statement in java

In this post we are going to delve into switch statement in java.

Switch statement in java

Java switch statement executes statements based on value of variable or expression. Switch statement evaluate expression first and then execute all statements following matching case label.

Syntax

switch(expression)
{
   case 1:
   // code goes here
   break;

   case 2:
   // code goes here
   break;
   
   case 3:
   // code goes here
   break;
   .
   .
   .
   .
   case N:
   // code goes here
   break;

   default:
   // default code goes here
}

switch statement in java

public class SwitchStatementJava
{
   public static void main(String[] args) 
   { 
      int year = 2;
      switch(year) // switch is evaluated only once
      {
         case 1:
             System.out.println("January");
             break; // break keyword is optional

         case 2:
             System.out.println("February");
             break;

         case 3:
             System.out.println("March");
             break;

         case 4:
             System.out.println("April");
             break;

         case 5:
             System.out.println("May");
             break;

         case 6:
             System.out.println("June");
             break;

         case 7:
             System.out.println("July");
             break;

         case 8:
             System.out.println("August");
             break;

         case 9:
             System.out.println("September");
             break;

         case 10:
             System.out.println("October");
             break;

         case 11:
             System.out.println("November");
             break;

         case 12:
             System.out.println("December");
             break;

         default: // default keyword is optional
             System.out.println("Default case");
      } 
   }
}

Output :

February

Switch statement is used with “break” statement. Break statement aborts enclosing switch statement.

Also read – java program to swap two numbers

By using break statement, execution flow jumps to next line following switch statement.


NOTE :

  1. In the above example we can’t use break statement after default, because control itself come out of switch after default.
  2. Meanwhile there’s no harm using break statement after default.
  3. Duplicate case value are not allowed. Case value should be of same data type.
  4. Expression inside switch should result in constant value else would be invalid.
  5. Case need not have to be in order you can specify them in any order. Also, it need not be in ascending order.
  6. In switch statement variable used are byte, short, integer, strings and enums.
  7. We can have any number of case statements within switch.
  8. Data type for a case must be same.
  9. We can also use characters in switch statement.
  10. Valid expressions for switch statement are as below,
Valid: switch(5 + 6 + 56) switch(5 * 6 + 7 % 8) 
Invalid: switch(pq + rs) switch(p + q + r)


Java switch statement can be used with primitive data types namely byte, short, char, and int. Let’s see a java program,

public class SwitchCaseDemo 
{
public static void main(String[] args)
{
int day = 3;
String dayString;
switch(day)
{
case 1: dayString = "Sunday";
break;
case 2: dayString = "Monday";
break;
case 3: dayString = "Tuesday";
break;
case 4: dayString = "Wednesday";
break;
case 5: dayString = "Thursday";
break;
case 6: dayString = "Friday";
break;
case 7: dayString = "Saturday";
break;
default: dayString = "Invalid day";
break;
}
System.out.println(dayString);
}
}

Output:

Tuesday


Switch case statement can be used with enumerated types. Enum types are special data type that enables for variable to be a set of predefined constants.

Enumerated data type are constants hence enum fields are in uppercase.

public class SwitchEnumDemo 
{
public enum Day
{
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}

Day day;
public SwitchEnumDemo(Day day)
{
this.day = day;
}

public void weekMethod()
{
switch (day)
{
case MONDAY:
System.out.println("week day monday");
break;

case TUESDAY:
System.out.println("week day tuesday");
break;

case WEDNESDAY:
System.out.println("week day wednesday");
break;

case THURSDAY:
System.out.println("week day thursday");
break;

case FRIDAY:
System.out.println("week day friday");
break;

case SATURDAY:
System.out.println("week end saturday");
break;

case SUNDAY:
System.out.println("week end sunday");
break;

default:
System.out.println("not a week day or week end");
break;
}
}

public static void main(String[] args)
{
SwitchEnumDemo day1 = new SwitchEnumDemo(Day.MONDAY);
day1.weekMethod();
SwitchEnumDemo day2 = new SwitchEnumDemo(Day.TUESDAY);
day2.weekMethod();
SwitchEnumDemo day3 = new SwitchEnumDemo(Day.WEDNESDAY);
day3.weekMethod();
SwitchEnumDemo day4 = new SwitchEnumDemo(Day.THURSDAY);
day4.weekMethod();
SwitchEnumDemo day5 = new SwitchEnumDemo(Day.FRIDAY);
day5.weekMethod();
SwitchEnumDemo day6 = new SwitchEnumDemo(Day.SATURDAY);
day6.weekMethod();
SwitchEnumDemo day7 = new SwitchEnumDemo(Day.SUNDAY);
day7.weekMethod();
}
}

Output:

week day monday
week day tuesday
week day wednesday
week day thursday
week day friday
week end saturday
week end sunday


Strings in switch statements

From Java 7 String object can be used in switch statement expression. Let’s see a java program,

public class StringSwitchDemo 
{
public static int getDay(String day)
{
int dayNumber = 0;
if(day == null)
{
return dayNumber;
}

switch(day.toLowerCase())
{
case "monday":
dayNumber = 1;
break;
case "tuesday":
dayNumber = 2;
break;
case "wednesday":
dayNumber = 3;
break;
case "thursday":
dayNumber = 4;
break;
case "friday":
dayNumber = 5;
break;
case "saturday":
dayNumber = 6;
break;
case "sunday":
dayNumber = 7;
break;
default:
dayNumber = 0;
break;
}
return dayNumber;
}

public static void main(String[] args)
{
String strDay = "thursday";
int returnDay = StringSwitchDemo.getDay(strDay);
if(returnDay == 0)
{
System.out.println("Invalid day");
}
else
{
System.out.println(returnDay);
}
}
}

Output:

4