Declaring variables
As you can see in the game cycle figure (Figure The Game Cycle), the game state is repeatedly modified through some kind of user input, and then displayed. In order for your program to modify or display the game state, it needs to be able to refer to it. It does this through variables.
A variable is a named instance. You introduce a variable through a declaration. A variable declaration is pretty simple: type-name variable-name. This expresses that variable-name names an instance of type type-name.
Here are some example declarations:
int numberOfRolls;
Date todaysDate;
Dice theDice;
Card trumpCard;
Card[] theDeck;
Card[] dealerHand;
int[] highScores;
String greeting;
TicTacToeSquare[][] board;
Each declaration above is also an example of a Java statement. A statement is simply a complete unit of code. For readability, statements are usually short, fitting on one line. Occasionally you will find it useful to extend statements over multiple lines. The main thing about statements is that they have to be terminated with a semicolon (;) character. This is so easy to overlook, it's worth emphasizing.
Statements must be terminated with a semicolon (;) character.
The ; is like the period (“.”) that you use to end a sentence. A period is your signal to the reader that the sentence is finished. Likewise, the semicolon is your signal to the compiler that the statement is finished. With one exception, which we'll come to later, terminating statements is the only thing you will do with semicolons.
When you declare a variable in your program, Java makes sure that the declaration obeys certain rules.
- The names of the type and the variable are valid. The Java term for these names is identifier. Valid identifiers do not contain spaces and do not start with numbers. They may be made up of letters, numbers, “_”, and “$”.
- Variable names cannot be one of Java's reserved words, also called keywords. These include the names of the primitive types, “class”, and a dozen or so others. You'll learn them as we go.
- The type exists. The type could be one built in to Java, or Java may have to search for it in your code or in a library of types, but it has to be able to find it.
- You have not already declared a variable with the same name in your program. As we learn more about how programs are structured, “in your program” will change to “in this part of your program” but the basic rule won't change: variable names must be distinct from each other.
Naming conventions
You may have noticed that the variable names in the examples above followed a pattern: they were composed of multiple words, began with a lower-case letter, and after the first word, the words began with a capital letter. Each programming language seems to have its own conventions on how to name variables. With another popular language, the convention is to separate words in variable names with the “_” character. You can do that in Java—it's legal—but it goes against convention.
Use the same conventions with class names as you do with variable names, but the first letters of class names are always capitalized (as you will notice in the examples above). Again, your program will work just fine if you do not capitalize class names, but you'll get along better with your fellow coders if you follow the naming conventions.
Another consideration in naming variables is how long to make the names. You'll find yourself balancing ease-of-typing (favoring short names) with descriptiveness (favoring long names). It may surprise you how quickly you forget what a variable is for when you come back to the code it's in only a day later. (There is a solution but it won't make sense now.)
A variable's value
We will frequently use the term value. When we talk about a variable's value, it means the state of the variable's instance. If we are talking about the variable numberOfRolls, which we might use to store the number of dice rolls the player has made so far, the value would start at 0, and then increase by 1 each time the player rolls the dice.
With variables of primitive types, the notion of value is not complicated. If we had this variable declaration and assignment statement:
int numberOfRolls; // declares numberOfRolls as an int variable
…and if we could inspect the variable's instance with a “memory scope”, and we knew how to interpret the bit patterns in the memory cells, we might see this (after six rolls):

But with reference variables—variables of class or array types—the notion of a variable's value is a little more complex. Suppose we have a Card variable card1, whose memory-instance picture would look like Figure A Card value:

The arrow from card1 to the instance of the Card class is the reference. If we had a memory scope, we would see the address of the Card instance in the memory location for card1—just some some number between 0 and however many bytes your computer's memory has. So the technically correct answer to the question “What's the value of card1?” is “the address of the Card instance it refers to.” But this is understood, so the most informative answer is “Ace of Clubs.”
You, the programmer, need to be aware of this relationship between a reference variable and the instance it refers to, and you need to be comfortable with sometimes being very explicit about the value of a reference variable being an address, and sometimes implicitly referring to the value of a reference variable being the value(s) of the instance it refers to. We will help by highlighting this distinction in future sections.
How do we change the value of a variable? The main way we do that is through the assignment statement, the subject of the next section.