A variable is just a name that we give to an area of memory so that we can refer to the contents by name.
So think of it like the mailboxes in an apartment block.
There's a bank of mailboxes in the lobby, and each one is labelled with the name of its owner.
When the post person delivers the letters, they put mine, for example, in the mailbox labelled Tim.
That makes it easier for me to get to my mail.
I just open my mailbox, rather than having to check all mailboxes.
Once again, there's a big difference, though, in the way humans and computers behave.
The post person wouldn't try to force a parcel into my mailbox,
because the mailbox slot's only big enough to take letters,
and things wouldn't go well if we tried to squeeze a large parcel in there.
But computers don't think like that,
so we have to tell them what kind of thing we can store in each of the variables that we use.
So let's see how that works with an example.
I'm going to come over here and I'm going to put a blank line on line 4, and starting on line 5, I'm going to type
var space tim : space String with a capital S
is equal to, in double quotes, Tim Buchalka.
Then on the line after that I'm going to type println left parenthesis, tim, right parenthesis.
So here what I've done, is created a variable called Tim,
and I've put the text, Tim Buchalka, in the area of memory that's now labelled Tim.
We've told the computer that this area of memory can hold string values,
So it's fine to store text there.
Now string, by the way, is the name given to types of variables that can store text,
and on the next line, line six, we're printing out Tim.
But instead of printing the three characters, T, I and M,
the computer goes off to the area of memory that we've labelled Tim and gets the contents.
It then prints those contents, not the name that we've given to that bit of memory.
So let's run the program to confirm that,
and you can see that it prints out my full name.
So on line five we've declared the variable Tim.
When we declare a variable, we tell the computer what type of information it can store.
In this case, it can store string objects, and we then give the variable a name, Tim.
And when we ran the program it printed my name at the end.
Often you'll keep the variable a value at the same time, which we've done here by using equals,
then the string that we want to assign to the variable,
but you can also assign the value later, if you want.
So let's just change the code for that. So I'm going to do press enter at the end of the string there on line five,
and I'm going to type Tim on line 6, equals Tim Buchalka. Now when I run that,
it does exactly the same thing.
The important thing is that the variable Tim must be declared before we assign a value to it.
Until we declare the variable, the computer doesn't know what kind of information it can store,
and also hasn't allocated any memory for it.
It's a bit like the post person trying to deliver a letter, but there's no mailbox with my name on it.
Being human, they can make decisions for themselves.
For example, they might ask the neighbours if anyone called Tim has moved in recently.
The computer won't make any decisions that you haven't programmed it to make, so you've just get an error.
So let's see what happens. What I'm going to do is delete the declaration on line 5,
or rather, what I'm going to do is not delete it. I'm going to turn it into a comment instead.
I'm gonna do that by putting two forward slashes, and noting that the color has changed, when I did that, to green.
And to see what happens, let's just run the program again,
and you can see we've now got two errors showing; we we've got unresolved reference,
and also, the same error showing again further down. So we've got basically two errors there.
So we've got two parts to this, we've got Main.kt, and at the top you can see where it says Main.kt.
So that's the name associated to this file.
We've got : 6 : 5. First one, the six is the line number and then the second part is the column number of the error.
So we're interested in the line number there. So it's telling us, basically there's an error on line six,
and we can see the code, a little arrow pointing to where it thinks this is a problem.
So go there on line six, and also further down on line seven,
and that's because the variable Tim has no longer been defined.
Basically, it's computer speak for I've got no idea what you mean by Tim.
So what's happened here is, we haven't declared the variable Tim,
because we've commented it it out and so Kotlin's got no idea what it's referring to.
And instead of deleting the declaration it turned it into a comment, as I mentioned.
Any lines starting with those two slashes, like the one I did there, is ignored by Kotlin,
and actually most programming languages.
It's a good idea to put comments in your code to explain what your code's doing,
and it's also a good way to temporarily disable lines of code so they aren't executed, like we did here.
Right, so I'm going to go back and uncomment that line again,
noting that the color changes. Run the program again and the errors disappear and we get the output back.
Alright, so I've declared Tim to be of type string, so that means it can only hold text values.
If we try to assign a number to it, it won't work.
So to see what I mean I'm going to delete the double quote Tim Buchalka and change that to a number,
an arbitrary number 32 there, and if I try and run the program again,
we've got an error now, the integer literal does not conform to the expected type string.
Here we're getting confirmation that we can't assign a number to a string variable. Kotlin doesn't allow that.
Now at this point, you might be tempted to get around that by enclosing the number 32 in double quotes
or speech marks. So let's do that.
You can run the program again.
There's no error now, and we're getting the 32 output in the output window,
but we haven't really fooled the computer though. Tim is still storing a string,
it's just made up of the two characters 3 and 2 now.
So it could be used to represent the number of my mailbox, but not my salary, for example.
In other words, we can't do sums with it.
If we want to treat it as a number, we have to effectively declare it to be a numeric type.
So let's have a go at doing that. So I'm going to add that code.
So we'll start on line 9, and we'll start with var space Tim's salary.
That's : space and Int with a capital I is equal to 32.
Next line, var monthly : Int again, with a space, equals timSalary multiplied by four.
The asterisk is the multiplication symbol. Then we're going to print them out, so print ln timSalary.
Then on the next line, println monthly. Alright.
So the most common Kotlin type for storing whole numbers is Int, short for integer.
By declaring Tim's salary, as I have on line 9, to be an int,
it's now treated as a number, and we can then do arithmetic with it.
and so I can multiply it by 4 to get my monthly salary, which I've done on line 10.
So there's a couple of things there. First, we've declared two variables to be of type Int,
which means that I can store whole numbers.
The second thing is that we can use the value of a variable in calculations,
then assign the result of that calculation to another variable.
And we can see that again on line 10, where the code multiplies the variable tim Salary by 4,
then assigns the result to the variable monthly. So when I run the program,
it correctly calculates my monthly salary and prints out both values.
And notice that it also prints the value of the variable tim out first, that's the first 32 in the output.
They look identical on the screen, but remember that one's a string and the others are number.
And to confirm that, if I try to multiply 10 by 4, on line 10, instead of Tim's salary,
we'll get an error, so I'm going to change the variable name to Tim.
So if we run that, we've now got an error, as you can see on the screen there.
So that error's a bit cryptic, None of the following candidates is applicable because of a receiver type mismatch.
What it means is that you can't multiply a string by an integer,
and actually you can't multiply a string by anything, but the computer doesn't do that kind of semantic reasoning.
It just sees a string and an int, so that's what it reports.
So I'm gonna put that back to what it was to get rid of the error,
and also take the opportunity of changing our first definition up here for Tim,
instead of 32, setting that back to what it was, Tim Buchalka.
We'll run the program again, and we've got it working as it should now.
Alright so let's end the video there.
In the next video in this playlist, we're going to talk about variable names,
and why I'm doing things like putting a capital S for the variable name timSalary.
So I'll see you in that next video.