OnJava8-Examples/control/StringSwitch.java
2015-09-07 11:44:36 -06:00

44 lines
894 B
Java

// control/StringSwitch.java
// ©2015 MindView LLC: see Copyright.txt
import static com.mindviewinc.util.Print.*;
public class StringSwitch {
public static void main(String[] args) {
String color = "red";
// Old way: using if-then
if("red".equals(color)) {
print("RED");
} else if("green".equals(color)) {
print("GREEN");
} else if("blue".equals(color)) {
print("BLUE");
} else if("yellow".equals(color)) {
print("YELLOW");
} else {
print("Unknown");
}
// New way: Strings in switch
switch(color) {
case "red":
print("RED");
break;
case "green":
print("GREEN");
break;
case "blue":
print("BLUE");
break;
case "yellow":
print("YELLOW");
break;
default:
print("Unknown");
break;
}
}
}
/* Output:
RED
RED
*/