This book is now obsolete Please use CSAwesome instead.

3.1. What is a Variable?

The following video is also on YouTube at https://youtu.be/pHgYlVjagmA. It explains what a variable is and gives a couple of real word examples of variables.

A variable is a name associated with a memory location in the computer. Computer memory can store a value and that value can change or vary. When you play a game, it will often have a score. Scores often start at 0 and increase. A score is a variable.

../_images/pongScore.png

Figure 1: A pong game in Scratch with a score shown in the upper left.

3.1.1. Variable Types on the Exam

There are two types of variables in Java: primitive variables that hold primitive types and object variables that hold a reference to an object of a class. A reference is a way to find the object (like a UPS tracking number helps you find your package). The primitive types on the Advanced Placement Computer Science A exam are:

  • int - which store integers (whole numbers like 3, -76, 20393)

  • double - which store floating point numbers (decimal numbers like 6.3 -0.9, and 60293.93032)

  • boolean - which store Boolean values (either true or false).

String is one of the object types on the exam and is the name of a class in Java. A string object has a sequence of characters enclosed in a pair of double quotes - like “Hello”. You will learn more about String objects in another chapter.

Note

Some languages use 0 to represent false and 1 to represent true, but Java uses the keywords true and false.

Check your understanding

You have attempted of activities on this page