What are Variables and Data Structures?

Variables are a key element to programming, and every programmer must understand them and how to use them. Variables are a way of storing, retrieving and modifying data within a computer program. There are two overarching types of variables; primitives and data structures. A variable is used to store any information needed by the program that is being executed, such as; a user’s name, the temperature of a kettle, the channel number of a television, etc.

A variable is stored within the computer’s memory when declared. Declaring a variable is when you create it and initilising a variable refers to the first time you assign the variable with a value. A variable that stores someone’s name is declared when you call a variable a name, and then initilised when you set the value of the variable to the person’s name, you can declare and initilise variable seperately or at the same time.

Primitive variables only assign one location in the computer’s memory, whereas data structures assign multiple values within the computer’s memory. a single number, or integer variable, such as the number ‘2’ would be a primitive variable whereas a list of integers would be a data structure. There are four main types of primitive variables; whole numbers or integers such as 2, decimal numbers or floats such as 2.5, booleans that can be ‘true’ or ‘false’, and characters that are a single unicode character such as ‘d’ or ‘1’. Characters can be single numbers too, but they are not treated as traditional numbers within a program or code. Some of these primitive, such as integers and floats, have minimum and maximum values, and there are other primitive types with bigger/smaller bounds. Choosing the correct primitive type usually comes down to the amount of memory you want to use.

Variables do not contain values themselves, they only point to where the values are stored within memory. Thinking of a street of houses can help visualise how variables and memory relate. Each primitive variable can point to any one house in the street, multiple variables can point to the same house. The house represents the memory of the computer, and within the house is where the value exists. A data structure points to multiple houses, or multiple locations in memory where values are stored.

Primitive Variables Types

NameSize (bits)Minimum ValueMaximum ValueExample Values
byte8-1281^27-65, 32, 17
char1602^16-165, 32, 17
short16-2^152^15-1-65, 32, 17
int32-2^312^31-1-65, 32, 17
long64-2^632^63-1-65, 32, 17
float322^-149(2-2^-23)·2^127-65.0, 32.7, 17.25
double642^-1074(2-2^-52)·2^1023-65.0, 32.7, 17.25
boolean‘true’ or ‘false’

Leave a Reply