desired. The data type of the variable is provided only once—when the variable is initially defined. After that, only the name of the variable is used. The com- piler remembers the data type. Also, once the data type is assigned, it cannot be changed: once an int, always an int. Once a double, always a double. This is true for all data types. Some sample assignment statements are as follows. int numberOfPets // define the variable numberOfPets = 5 // then assign a value double price = 4.99 // define and assign value together boolean isRaining // define the data type isRaining = false // then assign a value char letter = 'b' // define and assign value together a char //value is enclosed in single quotes Programmers often refer to the assignment operator as “gets” because the variable is receiving, or “getting,” a value. So the last statement above would be read as “letter gets b.” The above values assigned to the variables (5, 4.99, false, and b) are called literals. Assigning values in this manner is called hard-coding. Hard-coding is defining a variable value in the code. Java’s rules for literals that can be assigned to variables are summarized in Figure 4-5. Note that a literal for a long variable must be terminated with an l or an L. Because a lowercase l can easily be confused with the number 1, it is recommended to use the uppercase L, as in: long population = 923456670L Although two data types for real number are available, you may find it easier to use double by default. One of the reasons is that to define a float variable, you need to remember to add an F at the end: float taxRate = .15F whereas a double can be defined without any terminating character: double taxRate = .15 Goodheart-Willcox Publisher Figure 4-5. Java has rules for how literals are formed. Data Types Data Types Valid Literal Values Valid Literal Values Examples Examples int, byte, short Only numbers with an optional sign, no commas, dollar signs, or percentage signs (%). int numberPeople = 2534 int depth = –86 long Same as for other integer types except the number must be terminated with a lower- or uppercase L. long countryNo = 8325678990L double Decimal or scientific format with an optional sign no commas, dollar signs, or percentage signs. double price = 34.76 double gdp = 19.39E12 float Same as for doubles except the number must be terminated with a lower- or uppercase F. float discount = .20f char Any printable keyboard character enclosed in single quotes. A decimal value between 0 and 65,535. Any two-character escape sequence begun with a backslash for example, '\n' represents a new line character and '\t' represents a tab character. char wow = '!' char omega = 937 char tab = '\t' boolean One of the values true or false. boolean isPassing = true boolean isFailing = false Copyright Goodheart-Willcox Co., Inc. Chapter 4 Variables 83