This is a tricky question used to check your knowledge of current Java developments. Java 7 extended the capability of switch case to use Strings also, earlier java versions doesn’t support this.
If you are implementing conditional flow for Strings, you can use if-else conditions and you can use switch case if you are using Java 7 or higher versions.
public class Demo {
public static void main(String[] args) {
String department = “AKD05”;
switch(department) {
case “AKD01”:
System.out.println(“Finance”);
break;
case “AKD02”:
System.out.println(“Sales”);
break;
case “AKD03”:
System.out.println(“Production”);
break;
case “AKD04”:
System.out.println(“Marketing”);
break;
case “AKD05”:
System.out.println(“Operations”);
break;
default:
System.out.println(“None!”);
}
}
}
Output
Operations