What is a variable?

‹ Using Packer and Ansible (instead of Dockerfile) to create Docker Images | Unicode is Base 1114112 ›

You can visualize a variable1 as a box that contains a thing2. For example, this variable called x contains an integer 0:

x = 0

This other variable called most_recent_email contains a pointer (more below) which we draw with an arrow.

Beyond these basics, programming languages disagree what a variable is and can do. I will use variable to mean the green box, identifier to mean the name (like x or most_recent_email), and value to refer to a thing that goes inside the variable.

Declaration

Declaration creates a new variable out of thin air3. We'll draw a bomb inside the box since declaration by itself does not put a value in the variable, so errors will occur if you try to use what is inside.

LanguageCode
Cint x;
Rustlet x: i32;
Javaint x;
Javafinal int i;


Allocation

Allocation is like declaration, but creates a variable without an identifier. It also creates a pointer pointing at the variable.

LanguageCode4
Cmalloc(1000000000);
C++new int[1000000000];

Initialization

Initialization, which is often done at the same time as or immediately after declaration or allocation, is putting a value into a variable for the first time. You can initialize variables you previously declared:

LanguageCodeNote
Cx = 7;
Rustx = 7; 

You can declare and initialize at the same time (we introduce the lock drawing for the optional const or final which we will come back to in a second):

LanguageCode
Javafinal int i = 7;
C/C++int const i = 7;
PHP6$x = 7;
R6x <- 7

In a very common pattern, you can allocate, initialize, declare, and initialize all in quick succession. The x variable contains a pointer. Many languages call this kind of variable a reference:5

LanguageCode
Pythonx = 7
ES6let x = 7;
JavaInteger x2 = new Integer(7);
Haskelllet i = 7 in ....
C++int* x2 = new int; *x2 = 7;
C++int * const i2 = new int; *i2 = 7;

Assignment

Assignment puts a new value into a variable, wiping out the previous value. It is like erasing a chalkboard and writing something new on it.

Examples of assignment:

Languagecode
Cx = 9;
Javax = 9;
PHP$x = 9;

Also common is assignment of a variable that contains a pointer (many languages call this kind of variable a reference):

LanguageCode
Pythonx = 9
C++x2 = new int; *x2 = 9;

Not all programming languages and not all variables support assignment. Variables that don't support assignment are referred to in many programming languages as const. Using the i examples from above, all of these will cause an error:

LanguageCode
Javai = 9;
C/C++i = 9;
C++i2 = new int; *i2 = 9;

Identifier Aliasing

Some languages let you attach more than one identifier to the same variable. This is known as aliasing although PHP calls it "references".

LanguageCode
PHP$y =& $x;

Pointer Aliasing

Many languages let you create multiple pointers to a single allocated variable. This is also sometimes known as aliasing.

LanguageCode
Pythony = x
C++int* y2 = x2;

Footnotes

1 some variables in computers science are actually "free variables" which are different from what is described here. parameter assignment, closures, currying, unification, and variables in mathematics are all topics for another post. The word "variable" means too many things.
2 ultimately all variables store sequences of bits, but this is a topic for another post
3 actually, out of a big pool of memory
4 the compiler I tried optimized out these non-sensical examples, which you should never actually write into code
5 these examples don't actually store 7 -- for example in Python the value actually is made up of two separate values, an instance of PyObject_HEAD and a 7. In the Haskell example, the value is probably actually a Thunk that will evaluate to 7
6 PHP secretly does an allocation of a zval here, but tries to make it look like its variables directly store values.  R does something similar.

Subscribe to All Posts - Wesley Tanaka