AppDividend
Latest Code Tutorials

Java Switch Statement Tutorial | Switch Statement in Java Example

0

Java Switch Statement Tutorial | Switch Statement in Java Example is today’s topic. Java switch statement executes one statement from the multiple conditions. It is like an if-else-if ladder statement. The switch statement works with a byte, short, int, long, enum types, The String and some of the wrapper types like Byte, Short, Int, and Long. It comes with Java 7, you can use the strings in the switch statement.

Java Switch Statement

The switch statement is the multi-way branch statement. It provides an easy way to dispatch an execution to different parts of code based on the value of the expression. The expression can be a byte, short, char, and int primitive data types. It also works with the enumerated types (Enums in java), the String class and Wrapper classes.

#Key points of Switch Statement in Java

  1. There can be either one or N number of case values for a switch expression.
  2. The case value must be of the switch expression type only. The case value must be literal or constant. It does not allow the variables.
  3. The case values must be unique. In case of duplicate value, it renders compile-time error.
  4. The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
  5. Each case statement can have the break statement, which is optional. When control reaches the break statement, it jumps a control after the switch expression. If the break statement is not found, it executes the next case.
  6. The case value can have the optional default label.

See the following syntax.

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

  case value2:    
  break;  //optional   
    
  default:     
  code will be executed if all the cases are not matched;    
}

See the following code example.

class Conditionals {
  public static void main(String[] args) {
    String character = "Eleven";
    String StrangerThings;

    // switch statement with int data type
    switch (character) {
    case "Steve":
      StrangerThings = "Joe";
      break;
    case "Robin":
      StrangerThings = "Maya";
      break;
    case "Mike":
      StrangerThings = "Finn";
      break;
    case "Joyce":
      StrangerThings = "Winona";
      break;
    case "Eleven":
      StrangerThings = "Millie Bobby Brown";
      break;
    case "Dustin":
      StrangerThings = "Gaten";
      break;
    case "Will":
      StrangerThings = "Noah";
      break;
    default:
      StrangerThings = "Invalid day";
      break;
    }
    System.out.println(StrangerThings);
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
Millie Bobby Brown
➜  java

#Switch Statement is fall-through

The Java switch statement is a fall-through. It means it executes all the statements after the first match if the break statement is not present. See the following code example.

class Conditionals {
  public static void main(String[] args) {
    int number = 50;
    // switch expression with int value
    switch (number) {
    // switch cases without break statements
    case 10:
      System.out.println("10");
    case 50:
      System.out.println("50");
    case 60:
      System.out.println("60");
    default:
      System.out.println("Not in 10, 50 or 60");
    }
  }
}

See the following output.

➜  java java Conditionals
50
60
Not in 10, 50 or 60
➜  java

So, when it gets the first match found then onwards, it will execute all the following cases.

In the above case, whose value is ten will not executed because it does not match with 50.

#Java Nested Switch Statement

See the following example of Nested Switch Statement.

class Conditionals {
  public static void main(String args[]) {
    char branch = 'S';
    int schoolYear = 11;
    switch (schoolYear) {
    case 10:
      System.out.println("All subjects");
      break;
    case 11:
      switch (branch) {
      case 'C':
        System.out.println("Accounting");
        break;
      case 'S':
        System.out.println("Chemistry");
        break;
      case 'A':
        System.out.println("Philosophy");
        break;
      }
      break;
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
Chemistry
➜  java

In the above example, we have selected the 11th year and character S, that is why it is returning Science.

#Java Enum in Switch Statement

Java allows us to use enum in the switch statement.

See the following programming example.

class Conditionals {
  public enum Day {
    Sun, Mon, Tue, Wed, Thu, Fri, Sat
  }

  public static void main(String args[]) {
    Day[] DayNow = Day.values();
    for (Day Now : DayNow) {
      switch (Now) {
      case Sun:
        System.out.println("Sunday");
        break;
      case Mon:
        System.out.println("Monday");
        break;
      case Tue:
        System.out.println("Tuesday");
        break;
      case Wed:
        System.out.println("Wednesday");
        break;
      case Thu:
        System.out.println("Thursday");
        break;
      case Fri:
        System.out.println("Friday");
        break;
      case Sat:
        System.out.println("Saturday");
        break;
      }
    }
  }
}

See the following output.

➜  java javac Conditionals.java
➜  java java Conditionals
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
➜  java

#Java Wrapper in Switch Statement

Java allows us to use the four wrapper classes: Byte, Short, Integer, and Long in the switch statement.

class Conditionals {
  public static void main(String args[]) {
    Integer age = 18;
    switch (age) {
    case (16):
      System.out.println("You are under 18.");
      break;
    case (18):
      System.out.println("You are eligible for vote.");
      break;
    case (65):
      System.out.println("You are senior citizen.");
      break;
    default:
      System.out.println("Please give the valid age.");
      break;
    }
  }
}

See the output.

➜  java javac Conditionals.java
➜  java java Conditionals
You are eligible for vote.
➜  java

Finally, the Switch Statement in Java Example is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.