74 Part II: Writing Your Own Java (Vps web hosting) Programs

74 Part II: Writing Your Own Java Programs Listing 4-1: Using a Variable amountInAccount = 50.22; amountInAccount = amountInAccount + 1000000.00; The code in Listing 4-1 makes use of the amountInAccountvariable. A variable is a placeholder. You can stick a number like 50.22 into a variable. After you place a number in the variable, you can change your mind and put a different number into the variable. (That s what varies in a variable.) Of course, when you put a new number in a variable, the old number is no longer there. If you didn t save the old number somewhere else, the old number is gone. Figure 4-1 gives a before-and-after picture of the code in Listing 4-1. After the first statement in Listing 4-1 is executed, the variable amountInAccounthas the number 50.22 in it. Then, after the second statement of Listing 4-1 is executed, the amountInAccountvariable suddenly has 1000050.22 in it. When you think about a variable, picture a place in the computer s memory where wires and transistors store 50.22, 1000050.22, or whatever. In the left side of Figure 4-1, imagine that the box with the number 50.22 in it is surrounded by millions of other such boxes. Before executing After executing amountInAccount = amountInAccount = amountInAccount + 1000000.00; amountInAccount + 1000000.00; amountInAccount amountInAccount 50.22 50.22 1000050.22 Figure 4-1: A variable (before and after). Now you need some terminology. The thing stored in a variable is called a value. A variable s value can change during the run of a program (when Meredith gives you a million bucks, for instance). The value that s stored in a variable isn t necessarily a number. (You can, for instance, create a variable that always stores a letter.) The kind of value that s stored in a variable is a variable s type. You can read more about types in the section Understanding the Types of Values That Variables May Have, later in this chapter. A subtle, almost unnoticeable difference exists between a variable and a variable s name. Even in formal writing, I often use the word variable when I mean variable name. Strictly speaking, amountInAccountis a variable name, and all the memory storage associated with amountInAccount(including the
Here java web hosting you will find professional-grade java web hosting at affordable prices.

Leave a Reply