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) {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");
}
}
}
package r4rjdk7;
public class StrInSwt {
public static void main(String[] args) {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");
}
}
}