Java Switch Statement with String Example



Java Switch Statement with String Example demonstrates how to use a switch statement with String in Java.

Java switch Statement with String Example

Beginning with JDK 7, we can use a string to control a switch statement. 
The below example demonstrates the use of a string to control a switch statement.
package net.javaguides.corejava.controlstatements.switchcluase;

public class StringsSwitch {
    public static void main(String args[]) {
        String str = "two";
        switch (str) {
            case "one":
                System.out.println("one");
                break;
            case "two":
                System.out.println("two");
                break;
            case "three":
                System.out.println("three");
                break;
            default:
                System.out.println("no match");
                break;
        }
    }
}
Output:
two

Comments