Java switch case statement is the same as if else-if statement but are more efficient, lighter and offers better code readability.

Therefore, it is a good practice to use Java switch in replacement of a long if else-if ladder.

Further, if we use switch instead of long-chained if-else, the compiler can optimise the switch statement in a lookup table and perform compile-time checking for literals when dealing with enumerations.

Switch case executes statements depending on the expression we provide. We can use various data types in switch cases. For example, byte, short, char, int, long, enum types, strings and wrapper classes(byte, short, Integer and long).

Features of Java Switch

  • Java Switch expressions have cases.
  • There can be n number of cases.
  • We can not use variables as case values. These case values are constants or literals.
  • Cases values should be unique.
  • Break statement after each case is not mandatory. If we do not provide a break fall-through(discussed in the later part) might occur. nested switch case is also possible.
  • We can provide a default case. Default case executes only when no other cases match.

Switch Case Syntax:

switch(expression)
{
case value1:
//statement
break;

case value2:
//statement
break;

.
.
.
default:
//statement

}

The following image will help in better understanding.

switch statement java

switch statement java

Let’s take a look at different coding examples. After all, coding is very important.

Example 1: Java Switch case using int


public class SwitchCaseExample1 {

	public static void main(String[] args) {
		int dayOfWeek=2;
		switch(dayOfWeek)
		{
		case 1:
			System.out.println("SUNDAY");
			break;
		case 2:
			System.out.println("MONDAY");
			break;
		case 3:
			System.out.println("TUESDAY");
			break;
		case 4:
			System.out.println("WEDNESDAY");
			break;
		case 5:
			System.out.println("THURSDAY");
			break;
		case 6:
			System.out.println("FRIDAY");
			break;
		case 7:
			System.out.println("SATURDAY");
			break;
		default:
			System.out.println("WRONG CHOICE");
			break;

		
		}

	}

}

Output

MONDAY

Example 2: Switch case using strings


public class SwitchCaseExample2 {

	public static void main(String[] args) {
		String monthName="january";
		switch(monthName)
		{
		case "january":
			System.out.println("1st month of the year");
			break;
		case "june":
			System.out.println("Middle month of the year");
			break;
		case "December":
			System.out.println("12th month of the year");
			break;
		
		default:
			System.out.println("wrong choice");
			break;

		
		}

	}

}

Output

1st month of the year

Java Switch Case Fall through

Fall through occurs when we don’t provide a break statement in the switch case. In this case, the flow of control will fall through to subsequent cases until the break statement is encountered.

Example 3: To demonstrate fall through


public static void main(String[] args) {
		//Program -example of fall through
		char grade='B';
		switch(grade)
		{
		case 'A':
			System.out.println("Excellent");
			break;
		case 'B':
			System.out.println("Very Good");
		case 'C':
			System.out.println("Good");
			break;
		case 'D':
			System.out.println("Just Pass");
			break;
		case 'E':
			System.out.println("Better luck next time");
			break;
		default:
			System.out.println("wrong choice");
			break;

		
		}

Output

Very Good
Good

In the above code, it’s clearly visible that case-B and case-C both have executed. This happens because we have not used the break
statement in case-B so fall through occurs there.

Example 4: Code to demonstrate the use of a Java nested switch case in java.


public class NestedSwitchCase {

	public static void main(String[] args) {
		int age=16;
		String vehicle="gear";
		switch(age)//License eligibility test
		{
		case 10:
			System.out.println("Not eligible for driving license");
		break;
		case 16:
			switch(vehicle)
			{
			case "nonGear":
				System.out.println("You are eligible for license without gear");
				break;
			case "gear":
				System.out.println("You are not eligible  for license with gear");
				break;
			default:
				System.out.println("Wrong choice");
			}
		break;
		case 18:
			System.out.println("eligible for all kind of driving license");
		break;	
		default:
			System.out.println("Wrong Choice");
		}

	}

}

Output

You are not eligible  for license with gear

Example 5: Use of wrapper class in the switch case


public class SwitchCaseWrapperClass {

	public static void main(String[] args) {
		Integer year=2;//using wrapper class Integer
		switch(year)
		{
		case 1:
			System.out.println("you can participate only in  coding events");
			break;
		case 2:
			System.out.println("you can participate in coding and gaming events");
			break;
		case 3:
			System.out.println("you can participate in coding, gaming and robotics");
			break;
		case 4:
			System.out.println("you can participate in all events");
		}

	}

}

Output

you can participate in coding and gaming events

✌️ Like this article? Follow us on Facebook and LinkedIn. You can also subscribe to our weekly Feed