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 process differently, often using a lookup table for case values.