How is a variable stored in a computer memory ?
For example: when we specify
int x = 15;
Typically the computer in its ram allocates a 4 bytes chunk of memory, storing the value 15 in the form of 0's and 1's in the allocated 4 bytes and x refers to the address of the 4 bytes allocated memory.
Now, my doubt is how x refers to the memory location. Where and how the reference is stored ?
When we use x how it knows to refer to that memory location.
Is this different for different languages like java and python ?
Is this different for different data types ?
I shall be glad if you can make videos or blog to clarify this.
Typically the computer in its ram allocates a 4 bytes...
The whole sentence is completely correct. Only, there are different types of allocation. The simplest one is that the computer has, in a certain moment, an amount of free and contiguous memory. When a program starts, all that (free and contiguous) memory is given to the new program. It is completely responsibility of the program to manage the memory, and to not write outside that zone of memory. This is how DOS (real mode) works - very simple. The program (well, its runtime) is only informed about the starting and ending addresses of the zone of memory. It "maps" some "base" register into that ram, so as, for example, address 0 refers to the start address, where perhaps the program knows the variable X resides. More advanced operating systems go beyond, for example with mechanisms to block a program trying to read or write outside its allowed ram. Even beyond, more sophisticated OS's can give a program some ram initially, and then more ram when the program asks it.
Now, my doubt is how x refers to the memory location. Where and how the reference is stored ?
Typically the reference is not stored at all in the (compiled) program. What you (the programmer) call "X", for the program simply becomes an address. First variable address 0, second variable address 4, and so on. Debug symbols, when not stripped off, keep track of the "high level" names given to addresses; but they are used by debuggers and are not technically part of the "program".
When we use x how it knows to refer to that memory location.
Let's write a simple C program:
int x = 15;
int y = 20;
int s;
s = x+y;
The compiler sees "int x" and assigns to x the address 0. Then sees "int y" and assigns it address 4. Then address 8 to "s". Of course these addresses are remembered through the whole process of compilation. Meanwhile it sees those "x=15" and "y=20", and so outputs these instructions:
"store an integer of 4 bytes, value 15, at address 0"
"store an integer ..., 20, at address 4"
In the end it sees "s=x+y" and outputs this:
"take in the left hand the int (4 bytes) value at address 0"
"take in the right hand the int value at address 4"
"pour the left hand in the right hand"
"pour the right hand in 4 bytes at address 8"
As you see, there is no more x y or s, only addresses (this is a very simplified explanation).
Is this different for different languages like java and python?
Yes, but not too much. The name of a variable always refer to an address. Pure interpreted languages have to keep track of the name, because there is no separation between compilation and execution, and midway languages like java (I say midway because java is both a compiler and an interpreter) can do even more complicated things.
Is this different for different data types?
For simple types, no. In the C language, if we don't consider pointers and heap, all the variables are treated the same, only their length changes. An int can be 4 bytes long, a char one byte, an array has a length specified by the programmer, but the variables always refer to the first location of the buffer containing the value. The compiler keeps, for every variable, its address, its type, its length, so it can know what to do (what instructions to compile) when it encounters the variables names in the source.
It is very simple in concept. The compiler reads the text of the source. Every time it encounters a variable declaration it assigns it the first free address, then increment that by the size of the variable. At the begin, this address is 0. After the compiler reads a "int x;", the address of x becomes 0 and the "current" address is incremented (for an int, probably 4 or 8). Variable names, their type and their addresses are kept, and form a lookup table. This table is used to check that you don't declare an identifier twice, and is used to know where to read and write when the source refers to an already declared variable. If the source references an undeclared variable, the compiler complains because it doesn't find the variable in the lookup table. Note aside: you posted a link to a video which talks about pointers; pointers are also variables, but they introduce other concepts. What I said about addresses applies to pointers too, in the sense they are simply variables, but their value is used differently - and I don't understand why you insist on them.