Chapter 2

Variables: What is a Variable?

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.

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

Variable Types

There are three types of variables in java.

  • 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 and it 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.