ELI5: What are variables in computer science, in general?

First let's review how people remember things, real quick. If I say "four", you have a memory of what four is- a whole number, decimal number system, things like that. You might just use it more simply, but you've already internalized a whole list of rules for what makes sense with a number like that. Like if someone asks for five things, you respond with "hey wait that's an error, I only have four things." Same with letters. And what is a word, really, but a bunch of letters in a row?

So, when we made computers, we ran into a problem. This hunk of electric conductive metal we made doesn't know what a bloody number is, does it? Or a letter. Or how to think.

Uh, so we actually have to sit there and tell it what numbers and letters ARE. And we do that by referencing it's memory- yes, the actual physical memory. That memory is in little chunks and when we want the computer to remember a number, we tell it "Hey grab that open chunk of nothing there and reserve it for this number I want. And I'm gonna call that number "HamburgerStock."" And the computer says "Ok, I've reserved 8 memory chunks that say 00000000 for your new variable."

But how did it know to do 8 of them? And when I tell it my stock is 234, it becomes 00000234. How does it know what to even do with that later- if I tell it to do math for instance?

That's because when we made the programming language, we told it ahead of time how much memory to allocate for a variable type and what it's going to DO later if it sees other symbols- those are the rules. In summary (In ELI5 speak), variables are Memory and Rules and a Label.

Later if I write code that says add 3 to my hamburger stock, it knows what to do. Locate the memory, locate the ones column, add three, carry over any extra to the tens, et cetera. But it gets even more interesting because for every variable type there's different rules.

Some numbers need to go negative. That means in memory, it needs to remember that there should be room for a dash at the beginning. Like -0000000. We only have seven digits for our number now, because of all the new rules for negative numbers. Some numbers need a decimal point, which is a different variable type.

We even do this with letters, later. Strings aren't basic data types, but you can add them using a + sign. Or rather, the term is "concatenate", but you just end up with a big ole string of them back to back.

So check this out. What if I have four variables? Two are an integer called "Price" and "Tax" and they have 12 and 8 in them. If I ask the computer "what's price + tax", it'll respond with 20. The next two variables are strings called "whoops" and "uhoh". They also have 12 and 8 in them. What happens if I ask the computer "what's whoops+uhoh"? If you thought it will respond with 20, that's not right. Words don't have the same rules numbers do. The computer will respond with "128" because that's what we told it to do with words and letters.

("But aren't 12 and 8 numbers?" Actually, there's a catch to that. There is a big functional difference between the character 8 and the integer number 8. Both of those exist! They're separate entities! That's why you can name video game characters things like "xxWarrior624xx", because the 624 aren't numbers there.)

So, variables are basically three things. They are a location in physical memory, to store something. They're a label, so we can find the bloody thing. And they're a set of rules, so it knows what to do with them.

So when we write "int x = 4;" we're saying "make a new integer variable labeled x and set it to 4." And also "because that's an integer, if something needs to be done with it, reference all the rules we wrote for integer numbers."

All variables are like this, really. Char means "a single character." A string is literally "a bunch of Characters." A double is "A decimal number with a decimal point." A float is "A decimal number with a decimal point.

...

Those two (double and float) differ in the amount of memory assigned.

That's sort of it for the ELI5 version at any rate. I glossed over a lot of details. Like, a lot of them. I'm fairly certain I got the memory allocation of an int wrong, actually. But that's basically variables- memory with a label so the computer knows what to do with it later. Because it doesn't know until we tell it. Computers are kind of dumb like that. They really only do what we tell them to.

/r/explainlikeimfive Thread