Java 7 features: String in switch case usage tutorials with examples

String in switch case tutorials

Java 7 language has introduced using string in switch case. Prior to java 7 version, Switch case allows only Integer, byte, short, char variables and enumeration values only. Support of String is not a great language
feature but a handy for developers to use strings directly in switch case and simplify the developers in readability.
This post is my first post on java 7 features; I will do more posting on java 7 features in coming weeks.
Let us see the following code

public class SwitchWithStringsDemo {
    public static void main(String[] args) {
        int days = 1;
        String s="one";
        String numberValue;
        switch (s) {
            case 1:  numberValue = "ONE";
                     break;
            case 2:  numberValue = "TWO";
                     break;
            case 3:  numberValue = "THREE";
                     break;
            default: numberValue = "Invalid number";
                     break;
        }
       
    }
}
What will happen if we compose the above program in eclipse/net beans using java 1.7 version, so let us see the output of the above program in command line too

Eclipse error for usage of String in switch case below jdk 1.7 version


If we use directly use String in switch case, eclipse /net beans code editor throws following error.
Can not switch on a value of type string for source level below 1.7. Only convertible int values or enum constants are permitted.

Compile time error with using String in switch case


When java program using strings in switch is compiled with below java7 version, below error is thrown on command line.
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)

Advantages of Using String in Switch case feature in java 7.


Alternative to Switch case is if else statements but it is not that much readable as well as confusing developer in terms of more lines of code and cyclomatic complexity is increased. Cyclomatic complexity is method to identify the complexity of code
We have different alternative methods for using in Strings in switch case

Alternative to strings in Switch case using if else below java 1.7.


This is not an exact alternative to String usage in Switch case, but an alternative to switch case.
public class IfElseDemo {
 public static void main(String[] args) {
  String value = "ONE";
  if ("ONE".equals(value)) {
   System.out.println("Message One");
  }
  if ("TWO".equals(value)) {
   System.out.println("Message One");
  }
  if ("THREE".equals(value)) {
   System.out.println("Message One");
  }
 }
}

Alternative to String in Switch case example below jdk 1.7 using enums


Prior to java 7, enum’s can be mapped to switch case and below code specifies the usage of enum.
public class AlternativeToStringSwitch {

 public enum Daily {
  ONE, TWO, THREE;
 }

 public static void main(String args[]) {

  String str = "ONE";

  switch (Daily.valueOf(str)) {
  case ONE:
   System.out.println("one");
   break;

  case TWO:
   System.out.println("two");
   break;

  case THREE:
   System.out.println("three");
   break;

  }
 }
}

Example of String in switch case in jdk 1.7 with tutorials


public class SwitchStringDemoDemo {
    public static void main(String[] args) {
        int days = 1;
        String string="one";
        String numberValue;
        switch (string) {
            case "one":  numberValue = "ONE";
                     break;
            case "two":  numberValue = "TWO";
                     break;
            case "three":  numberValue = "THREE";
                     break;
            default: numberValue = "Invalid number";
                     break;
        }
       
    }
}
In java 7, variable of Strings are assigned and can be referenced in Switch case. Case Strings values are constants as like before in jdk6 version.

Null check for Strings in Switch case in java 7


As like other java code, if null is passed as String in Switch case, NullPointerException is thrown.
We have to handle the null checks for strings passed.
Please click +1 if you like this post.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.