Example & Tutorial understanding programming in easy ways.

What is the String inside switch feature of java 7?

One of the new features added in Java 7 is the capability to switch on a String.

Example :-

The way we use before jdk 7

package r4rjdk7;

public class StrInSwt {

public static void main(String[] args) {

   String color =
"red";

   if (color.equals("red")) {

     System.out.println("Color is Red");

       } else if (color.equals("green")) {

        System.out.println("Color is Green");

       } else {

       System.out.println("Color not found");

      }

   }

}


After jdk 7


package r4rjdk7;

public class StrInSwt {

public static void main(String[] args) {

    String color =
"red";

    switch (color) {

   case "red":

     System.out.println("Color is Red");

       break;

      case "green":

      System.out.println("Color is Green");

    break;

      default:

     System.out.println("Color not found");

    }

  }

}



Read More →