This book is now obsolete Please use CSAwesome instead.
3.2. 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.
To create a variable, you must tell Java its type and name. 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.
To declare (create) 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).
Here is an example declaration of a variable called score.
int score;
The value of score can be set later as shown below. Run the following code to see what is printed.
Check Your Understanding
public class Test2 { public static void main(String[] args) { int numLives; numLives = 0; System.out.println(numLives); double health; health = 8.5; System.out.println(health); boolean powerUp; powerUp = true; System.out.println(powerUp); } }
public class Test2 { public static void main(String[] args) { int numLives; numLives = 0; System.out.println(numLives); double health; health = 8.5; System.out.println(health); boolean powerUp; powerUp = true; System.out.println(powerUp); } }
Note
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.
Here is an example that shows declaring a variable and initializing it all in a single statement.
int score = 4;
Run the following code to see what is printed.
Check Your Understanding
public class Test2 { public static void main(String[] args) { int numLives = 0; System.out.println(numLives); double health = 8.5; System.out.println(health); boolean powerUp = true; System.out.println(powerUp); } }
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. Try to fix the following code to compile and run.
Check Your Understanding
3-2-7: Fill in the following: [blank] age = [blank]; to declare age to be an integer and set its value to 5.
3-2-8: What type should you use for a shoe size like 8.5?
3-2-9: What type should you use for a number of tickets?
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.