
1-59863-275-2_CH02_31_05/23/06 CHAP2 TE R VARIABLES, DATA TYPES, AND SIMPLE IO n this chapter, you learn how to use variables, data types, and standard Iinput/output (IO) to create interactive applications. You start by learning what variables are and what you use them for. Then you learn about prim- itive data types and how you can employ them to define variables. After you understand variables, you learn how to work with numbers and mathematical operators. Following your work with numbers, you learn about the String data type. Using the String data type, among other things, you can write programs that accept user input. To cap things off, you put all this knowledge together to build an application that generates standard output, accepts user input, and processes strings and numbers. Topics in this chapter include the following: • Creating a Name Game application • Declaring and naming variables • Using special characters • Creating a Math Game application • Creating a String Game application • Accepting command-line arguments 1-59863-275-2_CH02_32_05/23/06 32 Java Programming for the Absolute Beginner THE NAME GAME APPLICATION The skills you acquire in this chapter allow you to create an interactive application. Among other things, an interactive application allows you to process information that you type at the keyboard. This information is called input. The application processes this information and generates output, which in this instance you see displayed on your monitor. The two forms of interaction together are often called IO (for input/output). A class called NameGame supports the activity of your IO application, so it is appropriate to call the application Name Game. Figure 2.1 illustrates the session involving the Name Game application. FIGURE 2.1 The Name Game application explores how to use IO. The Name Game application prompts you for your first and last names. It then changes the information you provide and generates output that shows the changes. To help you input information and see the results of the changes, it provides what is known as a user interface. A user interface is the part of an application that communicates with the users of the appli- cation. This portion of your application differs from what is known as the core processing module. The user interface takes care of IO; the core processing module takes care of changing the data. Together, these two activities characterize almost every Java application. VARIABLES AND DATA TYPES To work toward creating the Name Game application, a first step involves learning about variables and data types. A variable is a container that stores a specific form of data. Picture a variable as a box. Picture data as what you put in the box. You can put different things, of different sizes, in the box. When you work with a computer program, the situation is analo- gous, but there is a difference. When you work with a computer program, you must designate a specific size of box for every item of data you want to store. To accomplish this, you use a 1-59863-275-2_CH02_33_05/23/06 Chapter 2 • Variables, Data Types, and Simple IO 33 data type. A data type defines the size of the data a variable can store. Specific types of data include integers, floating-point numbers, and bytes, among others. To designate the data type, you employ one of a select group of keywords to define the type of the identifier. The identifier is the word you use to name a given variable. After you assign a data type to an identifier, you create what programmers generally recognize as a variable. The activity of assigning the data type to an identifier is known as declaring a variable. Here are three declarations of variables: int firstNumber; //declarations of variables float secondNumber; byte thirdNumber; The Java keywords int, float, and byte identify data types, and the three statements declare variables of the int, float, and byte types. The three expressions (firstNumber, secondNumber, and thirdNumber) constitute identifiers, but then after you associate them with data types, they become variables. Having declared a set of variables, you can then define them. When you define a variable, you assign a value to it. Here are three statements that assign values to the three variables: firstNumber = 1984; //assignments of values to variables secondNumber = 98.6; thirdNumber = 8; After you assign a value to a variable, you are free to change the value. For example, you can change the value you assign to secondNumber from 98.6 to 102.4. To change the value in this way, you just assign the new number: secondNumber = 102.4; //change the value of the variables On the other hand, you cannot change the data type you assign to a variable after you have declared the variable. The variable secondNumber remains defined by the float type for the life of your program. Primitive Data Types You must use a data type to define every variable you create when you program with Java. For this reason, Java is known as a strictly typed programming language. All variables have data types. Java offers two forms of data types. One, which you see in the previous section, is known as primitive data types. Such data types are part of the core compiler definition of data that Java provides. Here are a few of the primitive data types, some of which you have seen before: 1-59863-275-2_CH02_34_05/23/06 34 Java Programming for the Absolute Beginner int, float, byte, bool, double, long, char Primitive data types allow you to create another general data type. This other general form of data is known as a reference data type. This data type is also known as an abstract data type. You often use primitive data types to build reference data types, and the Java class hierarchy provides you with hundreds of ready-made reference data types. Here are a few of the refer- ence data types: Object, JApplet, Integer, Float, Byte, Double, Long, String To master Java, you must master both types of data. Variables you declare using primitive data types store data of the type you use to define your variable. In other words, as you saw in the previous section, secondNumber stores the value 98.6. Reference data types prove more complex. They do not actually hold a value. Instead, they store information about the place in the memory of your computer at which a value can be found. The place in memory is known as an address or reference. This is where the name reference originates. Reference data types receive extended discussion later on. Table 2.1 provides information on the primitive data types in Java. T ABLE 2.1 PRIMITIVE DATA TYPES Keyword Discussion Size in Bits byte small integer 8 short short integer 16 int integer 32 long long integer 64 char character 16 float floating point 32 double double floating point 64 boolean Boolean (true or false) 1 Four of the data types Table 2.1 lists (byte, short, int, and long) are integers. An integer is a number that does not have a decimal point. You can assign positive or negative values to an integer. You can also assign zero (0) to an integer. A byte integer accommodates numbers in the range of roughly –128 to 127. You can use this data type to keep memory use at a minimum. The short integer type is larger than the byte and can accommodate a number approximately half as large as a regular int value. A variable of the long type, like a variable of the double 1-59863-275-2_CH02_35_05/23/06 Chapter 2 • Variables, Data Types, and Simple IO 35 type, can accommodate extremely large numbers. The only stipulation is that such numbers cannot contain decimal values. The two data types that allow you to work with real numbers (numbers with decimal points) are known as floating-point types. These are the float and double data types. The float type is comparable in size to the int type. The double data type handles very large numbers. It is like the long data type in this respect. A third form of primitive data is of the char type. This type of data allows you to deal with letters of the alphabet and a fairly large set of other symbols. A code, known as Unicode, establishes a unique numerical value for each letter or symbol. To use a variable of the char type, you assign a letter to it. You enclose the letter in single quotation marks to indicate that it is a character (for example, ‘a’, ‘1’, ‘b’, ‘$’). The last type of data Table 2.1 lists, boolean, relates to true and false (Boolean) values. Java provides two keywords associated with this data type, true and false. The true keyword has a numerical value of 1. The false keyword has a numerical value of 0. You use the keywords in a literal way. Here’s an example: boolean testValue; testValue = true; Table 2.1 provides information relating to the size of the primitive data types. Generally, a bit is a 0 or a 1 in your computer’s memory. Eight bits makes a byte. An integer in this scheme of things, then, can store a number that is four bytes long. What this means in specific values is that a variable of the int type can consist of roughly 15 digits. To store precise scientific calculations, you use a double. A double can store a very large number. Understanding Literals In the definitions shown previously, you assigned literal values to variables.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages40 Page
-
File Size-