Computers represent ALL data using sequences of bits (zeros and ones). Data includes:

Variables store data within programs. The value of a variable can be set, read, and updated within a program.

The example below uses AP CSP pseudocode to avoid the intricacies of how specific programming languages store values and perform calculations. (The AP CSP pseudocode is used here to abstract irrelevant details.)

Simple variable example (in AP CSP pseudocode)

score ← 86
DISPLAY(score)

score ← score + 5
DISPLAY("then")
DISPLAY(score)

The code segment outputs:

86 then 91

How the programmer thinks

score ← 86 stores 86 in score.

DISPLAY(score) (first occurrence) displays the current value of score (86).

score ← score + 5 updates score to store 5 more than it did before (91).

DISPLAY(score) (second occurrence) displays the current value of score (91).

What the computer does

The exact details below vary by programming language. The overall behavior is the same in all languages.

score ← 86: The computer represents 86 using bits (binary). Using 8 bits, 86 could be represented as 01010110. The computer reserves memory to store the bits. The computer makes a note of which memory location(s) store the value of score and the type of the data the bits represent (in this case, an integer).

DISPLAY(score): The computer reads the bits 01010110 from the memory location(s) associated with score. The computer has noted that the bits represent an integer, so the computer displays 86.

score + 5: The computer reads the value of score, as in the previous statement. The computer represents the integer 5 in binary. Using 8 bits, 5 could be represented as 00000101. The CPU performs the addition in binary. Using 8 bits, the result of the calculation (91) could be represented as 01011011.

The statement score ← 91 updates the bits at the memory location(s) representing score to 01011011.

DISPLAY(score): The computer reads from the memory location(s) associated with score, which now stores the bits 01011011. The computer has noted that the bits represent an integer, so the computer displays 91.

Variables as abstractions

Programmers usually don’t care how a computer represents data, as long as it reliably stores and retrieves the data. Most programming language hide (abstract) the details of how data values are represented using bits. Abstractions hide complex details so a programmer can focus only on what is important right now.

(Many Java programmers neither know nor care that Java represents signed integers using 2’s complement.)

It is sometimes helpful to look more closely at details. As an example, integer overflow and floating point roundoff error occur because of how many programming languages represent integers and floating point numbers (numbers with decimal points).

(Looking at details that were previously hidden is known as working at multiple levels of abstraction.)

Same bits different values

In the example above, the bits 01010110 represent the integer value 86 because the programmer said they did. The same bits could also represent the character V, a small black and white image, or a path through a binary tree. The context in which the bits are stored and read determines the meaning.

Java uses Unicode to represent characters (letters, numbers, and symbols). Working with char values shows examples of converting between int and char. These conversions don’t change the bits stored in memory, only way in which the bits are interpreted.

Help and comments

Get help from AP CS Tutor Brandon Horn

Comment on Variables as abstractions