Читать книгу OCP Oracle Certified Professional Java SE 17 Developer Study Guide - Jeanne Boyarsky - Страница 187
Selecting switch Data Types
ОглавлениеAs shown in Figure 3.3, a switch
statement has a target variable that is not evaluated until runtime. The type of this target can include select primitive data types (int
, byte
, short
, char
) and their associated wrapper classes (Integer
, Byte
, Short
, Character
). The following is a list of all data types supported by switch
statements:
int and Integer
byte and Byte
short and Short
char and Character
String
enum values
var (if the type resolves to one of the preceding types)
For this chapter, you just need to know that an enumeration, or enum, represents a fixed set of constants, such as days of the week, months of the year, and so on. We cover enums in more detail in Chapter 7, including showing how they can define variables, methods, and constructors.
Notice that boolean
, long
, float
, and double
are excluded from switch
statements, as are their associated Boolean
, Long
, Float
, and Double
classes. The reasons are varied, such as boolean
having too small a range of values and floating-point numbers having quite a wide range of values. For the exam, though, you just need to know that they are not permitted in switch
statements.