Java Switch Statement Sample

This is a sample of how to perform conditional branching using the Java switch statement.

Table of Contents

sample What is the switch statement?
  Sample switch statement
  Sample of checking for null
  Fall through (if there is no break)
  Using if statements within switch statements

What is the switch statement?

switch(conditions){
  case value :
    processing
    break;
   ・・・
  default:
    processing
}
  • If the value of the condition matches the value of the case, the process underneath will be executed.
  • Break will break out of the switch statement, otherwise it will go to the lower decision (case/default).
  • default is executed when the value of the condition does not match the value of any case. It can be omitted, but it is better to write it.
  • If you are using JavaSE7 or later, you can use the switch statement to evaluate strings.
    However, if the value is null, a NullPointerException exception will be thrown, so it is better to make sure that the value is not null before using the switch statement.

Sample switch statement

public class Test1 {
	public static void main(String[] args) {
		String a = "2";

		switch (a) {
		case "1":
			System.out.println("one");
			break;
		case "2":
			System.out.println("two"); // two
			break;
		default:
			System.out.println("three");
		}
	}
}

The case "2" in line 9 matches the value of the variable a, so the process under it is executed.
The break in line 11 exits the switch statement.

Sample of checking for null

  String a = null;

  if (a == null) {
    System.out.println("null"); // null

  } else {
    switch (a) {
      case "a":
        System.out.println("one");
        break;
      case "b":
        System.out.println("two");
        break;
      default:
        System.out.println("three");;
    }
  }

The first line sets the variable to null.
Line 3 checks for null in the if statement.
If the value is not null, the switch statement is executed from the else statement in line 6.

Fall through (if there is no break)

  String a = "1";

  switch (a){
    case "1":
      System.out.println("one"); // one
			
    case "2":
      System.out.println("two"); // two
      break;
    default:
      System.out.println("three");
  }

In line 6, there is no break.
After processing case "1" in line 5, the processing of case "2" in line 7 is also executed.
This is called "fall through".
It is better not to use it because it is difficult to understand if you forgot to write break.

Using if statements within switch statements

You can use the if statement within the switch statement.

  int a = 3;

  switch (a){
    case 3:
      if (a % 2 == 0) {
        System.out.println("even");
      } else {
        System.out.println("odd"); // odd
      }
        break;
    default:
      System.out.println("Other");
  }

Line 5 uses an if statement after the case in the switch statement.
% is to find the remainder of the division.

Related Articles

Java if文 条件分岐を行うサンプル
Java while文のサンプル

△上に戻る