Monday , February 27 2017
Home / java / String in Switch in Java 7 Example

String in Switch in Java 7 Example

String in Switch in Java 7 is introduced to support for String data type in switch statement for easy maintenance. Developers tend to go with Switch statement to avoid having multiple if-else blocks and to make the code look more cleaner and readable.

String in Switch in Java 7

Prior to Java 7, Switch statement was supported only for data types int, short, byte, char, Integer, Short, Byte, Character and Enum Types. But compared to any other data type String was mostly widely used and Java could have provided it long back. Since String was not supported, developers were following below options to achieve what they need:

  • When switch started supporting Enum types, developers were using Enum as replacement for String in Switch.
public class EnuminSwitch {
    private enum Day {
        SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, SATURDAY, FRIDAY;
    }

    public static void main(String[] args) {
        String value = "SUNDAY";
        Day today = Day.valueOf(value);
        switch (today) {
        case SUNDAY:
        case SATURDAY:
            System.out.println("Today is weekend holiday");
            break;
        default:
            System.out.println("Today is weekday. Go to office");
            break;
        }
    }
}
  • Conventional way with if-else clauses
public class StringIfElse {
    public static void main(String[] args) {
        String value = "SUNDAY";
        if (value.equals("SUNDAY") || value.equals("SATURDAY")) {
            System.out.println("Today is weekend holiday");
        } else {
            System.out.println("Today is weekday. Go to office");
        }
    }
}

String in Switch

String in Switch in Java 7 has made the way of having a cleaner and maintainable code. Below program explains how to use String in Switch as a replacement for Enum and Conventional way. Switch in String uses equals() method of String class to compare the value passed

public class StringInSwitch {
    public static void main(String as[]) {
        String today = "SUNDAY";
        switch (today) {
        case "SUNDAY":
        case "SATURDAY":
            System.out.println("Today is weekend holiday");
            break;
        default:
            System.out.println("Today is weekday. Go to office");
            break;
        }
    }
}

String in Switch in Java 7 Good and Bad

  • Good:
    • Eliminates the complexity in writing Enum and if-else clauses
    • Using nested or more if-else clauses increases Cyclomatic complexity
    • More readable and maintainable
  • Bad:
    • Since Switch internally uses String.equals() method to compare value and cases, make sure you don’t pass the null in switch statement
    • Make sure you either follow same pattern to represent values, since Switch statement with String is case-sensitive.
      • Better option here is to use Constants class and refer varibles from it
    • Make sure your JDK is Java 7 and above, because if you use string in switch in lower versions, it will not throw compile time error, but gives RuntimeException.

Exception in thread “main” java.lang.RuntimeException: Uncompilable source code – strings in switch are not supported in

-source 1.6 (use -source 7 or higher to enable strings in switch)

at jdk7demo.StringInSwitchJava7.tradingOptionChooser(StringInSwitchJava7.java:34)

at jdk7demo.StringInSwitchJava7.main(StringInSwitchJava7.java:25)

About Rajesh Reddy Badam

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates.

Check Also

Type Inference in Java 7 Example

Type Inference in Java 7 is another great addition introduced to ease the developer to type …

Leave a Reply

Your email address will not be published. Required fields are marked *