MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

control/StringSwitch.java:7–39  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

5
6public class StringSwitch {
7 public static void main(String[] args) {
8 String color = "red";
9 // Old way: using if-then
10 if("red".equals(color)) {
11 System.out.println("RED");
12 } else if("green".equals(color)) {
13 System.out.println("GREEN");
14 } else if("blue".equals(color)) {
15 System.out.println("BLUE");
16 } else if("yellow".equals(color)) {
17 System.out.println("YELLOW");
18 } else {
19 System.out.println("Unknown");
20 }
21 // New way: Strings in switch
22 switch(color) {
23 case "red":
24 System.out.println("RED");
25 break;
26 case "green":
27 System.out.println("GREEN");
28 break;
29 case "blue":
30 System.out.println("BLUE");
31 break;
32 case "yellow":
33 System.out.println("YELLOW");
34 break;
35 default:
36 System.out.println("Unknown");
37 break;
38 }
39 }
40}
41/* Output:
42RED

Callers

nothing calls this directly

Calls 1

equalsMethod · 0.45

Tested by

no test coverage detected