Join our newsletter for the latest updates.
Java Program to Implement switch statement on strings

Java Program to Implement switch statement on strings

In this example, we will learn to implement the switch statement on strings in Java.

To understand this example, you should have the knowledge of the following Java programming topics:


Example: Implement the switch statement on Strings

// Java Program to implement String on switch statements in Java

class Main {
  public static void main(String[] args) {

    // create a string
    String language = "Java";

    switch(language) {

      case "Java":
        System.out.println(language + " is famous for enterprise applications.");
        break;

      case "JavaScript":
        System.out.println(language + " is famous for frontend and backend.");
        break;

      case "Python":
        System.out.println(language + " is famous for ML and AI.");
        break;

      default:
        System.out.println(language + " not found on record.");
        break;
    }
  }
}

Output

Java is famous for enterprise applications.

In the above example, we have implemented the switch statement on Strings. This feature was introduced in Java 7.