Declaring Variables in Java

Computers store all values using bits (binary digits). A bit can represent two values and we usually say that the value of a bit is either 0 or 1.

You have tell Java its type and name in order to make a variable. Creating a variable is also called declaring a variable. When you create a primitive variable Java will set aside enough bits in memory for that primitive type and associate that memory location with the name that you used. You have to tell Java the type of the variable because Java needs to know how many bits to use and how to represent the value. The 3 different primitive types are all represented using binary numbers (numbers that use base 2 with digits 0 and 1), but are represented in different ways. For practice converting between decimal and binary see http://forums.cisco.com/CertCom/game/binary_game_page.htm.

When you declare a variable, a memory location (sequential number of bits) is set aside for a variable of that type and the name is associated with that location. An integer gets 32 bits of space, a double gets 64 bits of space and a boolean could be represented by just one bit, but the amount of space isn’t specified by the Java standard.

../_images/typesAndSpace.png
Figure 1: Examples of variables with names and values. Notice that the different types get a different amount of space

To declare (make) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon (;). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).

Figure 2: How to Declare a Variable

Here is an example declaration of a variable called score.

The value of score can be set later as shown in the code below:

Think of the semicolon in Java like a period (.) in English. It is how you show the end of a sentence. You use a semicolon (;) to show the end of a Java statement. You will not be penalized on the exam if you forget the semicolon.

You can also optionally specify an initial value for the variable by adding an equals sign = followed by the value.

Figure 2: How to Declare and Initialize the Value of a Variable

Here is an example that shows declaring a variable and initializing it all in a single statement.

Run the following code to see what is printed.


*Note: The equal sign here = doesn’t mean the same as it does in a mathematical equation where it implies that the two sides are equal. Here it means set the value in the memory location (box) associated with the name on the left to a copy of the value on the right. The first line above sets the value in the box called score to 4. Also note that the variable has to be on the left side of the = and the value on the right. Switching the two is called assignment dyslexia.

This is an example of assignment dyslexia, when the student has put the value on the left and the declaration on the right side.

Mixed up Code Problems

The following method has the code to declare and initialize variables for storing a number of visits, a person’s temperature, and if the person has insurance or not. It also includes extra blocks that are not needed in a correct solution. Drag the needed blocks from the left area into the correct order (declaring numVisits, temp, and hasInsurance in that order) in the right area. Click on the “Check Me” button to check your solution.