Variables - A piece of memory that we can name whatever we want.

1. Variables

Part 2 of 7 - in the series:
Programming is so easy - The 6 concepts of programming you need to learn


You probably all know the famous Windows error messages that are as meaningful as a menu in Chinese characters.
 


What do these funny number sequences mean?

A computer stores all the data it wants to work with in the RAM (Read And Write Memory). Since there are many possible locations for storing data, there is a special logic for naming these locations in the RAM.

Think of it like this:
The long combination of numbers you see there is the address for a very specific place on the RAM bar in your computer. So the computer knows where to go to read or write data.



The problem with these "memory addresses" is that they are far too long and complicated for us. We would much rather use a simple word to say:
"my number is now stored in memory in the place called 'number_1'."
And this is where VARIABLES come into play.

Variables allow us to assign our own name to a memory location.
So when we create a variable named 'number_1' in our program, the computer remembers it in the background:

'number_1 'stands for the address '0x0000000025C2E42B'.

So we can work in programming with simple names for the complex memory addresses and do not have to worry about which memory address is actually hidden behind it.

But in most programming languages we also have to tell the computer WHAT we want to store in memory at this point.

From the very beginning, computers have always been designed to waste as little memory as possible and therefore we should only use as much memory for our variable as is really necessary. Because for storing a small number like "55", the computer needs less space than if we want to store the number "6545.23008796423424".

For this purpose, you can specify the so-called datatype for variables. So:
"what kind of data do I want to store in this variable?".
If we want to store only a small number (from 0 to 255) in the variable, we tell the computer this at the moment we mention our variable for the first time in our programme.

byte number_1

The key word 'byte' in front of the variable name tells the computer that we only need a very small amount of memory.
If we now want to store a larger number, we could tell the computer to do so:

int number_2

The key-word 'int' reserves for our variable 'number_2',twice as much space in memory as the key-word 'byte'.
Therefore we can store integers of up to 4294967295 in the variable 'number_2'.
Larger numbers need more space. And we have to tell the computer how much space we will need for the number.

But numbers that need a lot of space are not always BIG. Let us look at the following number:
0,00000000000123
This is a very small number with many decimal places.
Again, the computer must provide enough space in the RAM, because the many '0' after the decimal point must be stored somewhere.
A variable for such a decimal number (not an integer) could look as follows:

double number_3

The keyword 'double' reserves for our variable 'number_3', eight times as much space in memory as the keyword 'byte'.
In addition, the word 'double' contains the information that these are decimal numbers. The computer needs to know this to work with the variable correctly.

Simply said:
"we have to tell the computer if we have numbers without decimal places (integers) or if there are decimal places (decimals)".

You will notice that a computer is very accurate (not to say fussy) in the way it works with numbers.

For a computer a number does not only exist 1 time but in different ways:

byte number_1 = 5         A small integer with the value 5
short number_2 = 5        A medium integer with the value 5
int number_3 = 5            A large integer with the value 5
float number_4 = 5.0      A decimal number with only a few possible decimal places and the value 5
double number_5 = 5.0   A decimal number with many possible decimal places and the value 5
char number_6 = '5'        A letter indicating the number 5.

All these possible types of the number 5 are at first completely different things for the computer. Although the computer is able to calculate most of these different numbers with each other, this can often lead to problems.
You just can't match a letter with a number, even if that letter looks like a '5'.


As seen above, we can give a value to the variable name you have chosen yourself. e.g. 5 as shown above.
For this we use the '=' sign.
A number on the right side of the '=' sign is written into the variable on the left side of the '=' sign. And as we know that our variable name is only a placeholder for a piece of memory, the number is stored in the memory.

The name variable (substantiated adjective, feminine, changeable size) already lets us assume that we can change the number we have stored in the variable at any time.
So a part of our programme could look like this:

int number_1 = 55;
number_1 = 222;
number_1 = 9999;


(** The semicolon ";" at the end of a line, is in many programming languages something like the "." at the end of a sentence and ends the instruction **)
In the first line we tell the computer that we want to have a big integer called 'number_1' and that we want to put the '55' in it.
In the second line, we ask the computer to fill the memory location behind the variable 'number_1' with the value '222' and thus overwrite the number '55'.
The number '55' is now overwritten and irretrievably deleted.  In the third line again we overwrite the '222' with a '9999'.

There is no "trash bin" or "CTRL+Z" in programming.

Because variables only hide memory addresses with their values, we can also copy values between variables and therefore between memory areas.
In the following example we create 2 variables and copy the value of one variable into the other variable.

int number_1 = 5;
int number_2 = 77;
number_1 = number_2;


In this example we set the integer variable 'number_1' to '5' and the variable 'number_2' to '77'.
In the third line we tell the computer to copy the value of the variable 'number_2' (77) to the memory address of the variable 'number_1'.
Thereby the variable 'number_1' is set to '77' and its old value '5' is lost. The variable 'number_2' is not changed by this action.

Here again the broken down what the single parts of such a code instruction mean for a variable:


int number_1 = 5;
            
int                 [DATENTYPE_INTEGER]
number_1    [SELF_FOUND_NAME]
=                 [ASSIGNMENT_FROM_LEFT_TO_RIGHT].
5;                [THE_VALUE].


When we create a new variable in this way, we speak of a "declaration" in programming.

int number_1; <<< We 'declare' that from now on there is an integer variable named 'number_1'.


If we assign a value to the variable at the same time, we also speak of a "definition".

int number_1 = 55; <<<< We 'declare' that from now on there is the integer variable 'number_1' and 'define' for it the starting value '55'.


Please try to keep in mind that the 'datatype' ( int, float, byte ) of a variable only describes how much space it needs in memory and how the computer should read it (e.g. with or without decimal places). Once you have internalised this principle, you can also handle any data type ( image, music file, video ), because in the end it is always just a piece of memory on which we have stuck a small label with a name of your choice. 

Do not hesitate to pass on the series if you think it is useful or send me comments if you think I am wrong or have missed something.


 GERMAN VERSION


Contact me:
Kai Niklas - CEO decode GmbH
Mail: k.niklas@decode-it.de
Twitter: @Kai_Niklas
Skype: uwi2k2
Instagram: uwiworld



Kommentare

  1. Just some points for improvement:
    - Those addresses are point to virtual memory and not the physical RAM memory.
    - It seams to have some confusion on how floating point number works.
    - Data type only matter for the compiler/interpreter, CPUs don't know about types.


    AntwortenLöschen
  2. Thanx for the feedback !
    All your points are of course right.
    I try to simplify things to help people understand. :-)

    AntwortenLöschen

Kommentar veröffentlichen

Beliebte Posts aus diesem Blog

Place items in the hands of 2D characters in a 3D world

Technical Tile Data vs. Visible Tile Data