Java Reference
In-Depth Information
case "linen":
System.out.println("Linen selected");
// Set conditions for linen...
break;
case "wool":
System.out.println("Wool selected");
// Set conditions for wool...
break;
default: // Not a valid value for wash
System.out.println("Selection error");
break;
}
The wash variable here must be of type String — you will learn about the String class in Chapter 4.
The toLowerCase() method from the String class converts the string stored in wash to lowercase so the
switch works regardless of whether upper- or lowercase letters are used to specify the choice of wash.
The General Case of the switch Statement
The flowchart shown in Figure 3-4 shows the logic of the general switch statement.
FIGURE 3-4
NOTE While Figure 3-4 show the logic of the switch statement, the compiler may implement
the process differently, often using a lookup table for case values.
 
 
Search WWH ::




Custom Search