In this page we will explain what exactly are variables in a program and why do we need them.
As stated in the programming basics page – Variables are places in which we store information that is needed in order to perform the task.
It is fair to say that variables are the fundamental building blocks of any computer program. Any information that the program is using will have to be stored in a variable at some point in the program.
Technically, variables are places in the memory of a computer program that contain information that the program is using.
Variables are used in order to make calculations, store temporary information that is relevant to the current task the the program is executing, and in order to pass information between different parts of the program.
Every variable has a type and a name.
The variable’s type specifies what kind of information can be stored in the variable, and the variable’s name gives a name that we will use whenever we want to address the information stored in the variable, or when we want to store new information in the variable.
Basic variable types
The basic variable types are types that can store simple numeric data or single text characters.
In Java (which is the language we use for the beginners sections) there are 8 basic (primitive) types.
Here is a short description of the most common basic data types that we will use in this guide:
- int: Stores integers (non fractional) in the range of minus -231 (~ -2 Billion) to 231-1 (~2 Billion).
- long: Stores integers (non fractional) in the range of -263 to 263-1.
- double: Stores numbers that can include fractions (64-bit double-precision).
- boolean: Stores either ‘true‘ or ‘false‘ value, and is used in order to indicate the result of a certain condition.
- char: Stores a character, for example, the letter ‘e‘ is a character.
- String (Not a primitive type, but still pretty basic): Stores a “string” of characters, resulting in a word, sentence, or any other data that can be represented as a concatenation of characters.
Before we can use a variable, we have to declare it first. A variable declaration is actually informing the program that there is a variable with the specified name, and the specified type.
This operation causes the program to allocate a space in the memory that will be used in order to store and access the value of the specified variable.
Different data types require different amount of space.
The amount of space that variables require are usually measured in bytes or bits, where each byte is represented by 8 bits. Generally, one byte is the minimum size of a variable.
For example, if you look back in our programming basics main page, you will see that we say that we use 4 variables:
Declaring these variables will look like this:
int start = 1; int end = 10; int step = 1; int counter = start;
You can see that we used the int data type for all of our variables because all of them are going to store non-fractional numbers.
We will continue to talk about variables in the next programming basics pages, while explaining more basic programming concepts.