A variable can be assigned multiple values over the course of the program’s execution. However, a variable can have only a single value at any one time. These statements show that values can be assigned when the variable is defined and new values assigned later in the program: int reynaAge = 11 // reynaAge gets 11 int suriAge = 14 // suriAge gets 14 suriAge = 15 // suriAge is changed to 15 int benAge = suriAge // benAge gets 15 Also, any variable can be assigned the value of a variable that can hold a smaller value. For example, the value of a short variable can be assigned to an int or a long, or even to a float or double. Assigning String Literals Remember from Chapter 3 that the statement used for output was: System.out.println( "Hello World!" ) The message to output was placed inside quotation marks. The message in this statement is actually a String literal. A String literal is a sequence of characters enclosed in quotation marks (“). String is not a primitive data type. Rather, String is a Java class. Classes are discussed in detail in Chapter 6. When a String literal is placed inside parentheses in an output statement (System.out.println), the characters are outputted exactly as coded. When we put a variable name inside the parentheses, the value of the variable is outputted. With these statements: int numberOfBooks = 2000 System.out.println( numberOfBooks ) the output is: 2000 Although outputting the value of a variable is helpful, it might leave the user wondering what the 2000 means. Labels can be provided for data values using String literals in combination with the concatenation operator (+). The concatenation operator joins data values and String literals into one message for output. For example, with these statements: int numberOfBooks = 2000 System.out.println( "I have " + numberOfBooks + " books." ) the output is: I have 2000 books. Notice the concatenation operator is used twice. The concatenation operator is needed each time the message switches between a String literal and a variable. To insert spaces before and after the variable value, place them into the String literal, as shown in Figure 4-6. Space Space System.out.println( "I have " + numberOfBooks + " books." ) Goodheart-Willcox Publisher Figure 4-6. To have spaces appear before and after the variable’s value, they must be added to the code. Copyright Goodheart-Willcox Co., Inc. 84 Introduction to Computer Science: Java Programming