Variables and the Primitive Types
NAMES ARE FUNDAMENTAL TO PROGRAMMING. In programs, names are used to refer to many different sorts of things. In order to use those things, a programmer must understand the rules for giving names to things and the rules for using the names to work with those things. That is, the programmer must understandthe syntax and the semantics of names.
According to the syntax rules of Java, a name is a sequences of one or more characters. It must begin with a letter and must consist entirely of letters, digits, and the underscore character '_'. For example, here are some legal names:
N n rate x15 quite_a_long_name HelloWorld
Uppercase and lowercase letters are considered to bedifferent, so that HelloWorld, helloworld, HELLOWORLD, and hElloWorLD are all distinct names. Certain names are reserved for special uses in Java, and cannot be used by the programmer for other purposes. These reserved words include: class, public, static, if, else, while, and several dozen other words.
Java is actually pretty liberal about what counts as a letter or a digit. Java uses the Unicodecharacter set, which includes thousands of characters from many different languages and different alphabets, and many of these characters count as letters or digits. However, I will be sticking to what can be typed on a regular English keyboard.
Finally, I'll note that often things are referred to by "compound names" which consist of several ordinary names separated by periods. You've already seen anexample: System.out.println. The idea here is that things in Java can contain other things. A compound name is a kind of path to an item through one or more levels of containment. The name System.out.println indicates that something called "System" contains something called "out" which in turn contains something called "println". I'll use the term identifier to refer to any name -- single orcompound -- that can be used to refer to something in Java. (Note that the reserved words are not identifiers, since they can't be used as names for things.)
Programs manipulate data that are stored in memory. In machine language, data can only be referred to by giving the numerical address of the location in memory where it is stored. In a high-level language such as Java, names are usedinstead of numbers to refer to data. It is the job of the computer to keep track of where in memory the data is actually stored; the programmer only has to remember the name. A name used in this way -- to refer to data stored in memory -- is called a variable.
Variables are actually rather subtle. Properly speaking, a variable is not a name for the data itself but for a location in memory that canhold data. You should think of a variable as a container or box where you can store data that you will need to use later. The variable refers directly to the box and only indirectly to the data in the box. Since the data in the box can change, a variable can refer to different data values at different times during the execution of the program, but it always refers to the same box. Confusion canarise, especially for beginning programmers, because when a variable is used in a program in certain ways, it refers to the container, but when it is used in other ways, it refers to the data in the container. You'll see examples of both cases below.
(In this way, a variable is something like the title, "The President of the United States." This title can refer to different people at differenttime, but it always refers to the same office. If I say "the President went fishing," I mean that George W. Bush went fishing. But if I say "Hillary Clinton wants to be President" I mean that she wants to fill the office, not that he wants to be George Bush.)
In Java, the only way to get data into a variable -- that is, into the box that the variable names -- is with an assignment statement. An...