What level of importance should be placed on selecting identifiers for a Java program? SEC T ION 4.1 Identifiers and Data Types C hoosing names for data serves several purposes. Named data can be stored in memory while the program executes so the value can be retrieved as needed. Also, naming data allows a generalized algorithm to be created that works with any data. To use other data for a program, those other data values are simply stored in the names that have been defined. Using the circumference example, code can be written that multiplies the diameter by pi to calculate the circumference. The diameter is a variable in this case. Then, for each execution of the program, a different value can be stored for the diameter. Without changing the code, the program will calculate the correct circumference for the current value of the diameter. Learning Goals After completing this section, you will be able to: Select identifiers for data. Identify correct data types. Illustrate the process for defining variables in Java. Terms data type identifier primitive data type real number variable Identifiers A variable is a named memory location containing data that can change from one execution of the program to another. In some cases, a program might change the value of a variable as the program executes. In other cases, a variable may store data input by the user. Identifiers are names the programmer chooses for the variables in pro- grams. Identifiers are also used as names for programs and for any methods the program defines. For example, the main method was used in the previous chapter. The identifier is main. Java’s rules for creating identifiers are shown in Figure 4-1. As you can see, a name must start with a letter, underscore, or dollar sign. Numbers are allowed after the first character, but a name cannot start with a number. One important rule is that spaces are not allowed in names. Also important is that names are case-sensitive, meaning that number1, Number1, and NUMBER1 are all consid- ered to be different identifiers. Although an underscore is permitted anywhere in a name, a name consisting of only an underscore is not allowed. FYI Always remember that spaces are not allowed in Java names. Copyright Goodheart-Willcox Co., Inc. Copyright Goodheart-Willcox Co., Inc. 74 Introduction to Computer Science: Java Programming
Previous Page Next Page