1 00:00:05,120 --> 00:00:12,040 A variable is just a name that we give to an area of memory so that we can refer to the contents by name. 2 00:00:12,040 --> 00:00:15,900 So think of it like the mailboxes in an apartment block. 3 00:00:15,900 --> 00:00:21,040 There's a bank of mailboxes in the lobby, and each one is labelled with the name of its owner. 4 00:00:21,040 --> 00:00:26,320 When the post person delivers the letters, they put mine, for example, in the mailbox labelled Tim. 5 00:00:26,320 --> 00:00:28,740 That makes it easier for me to get to my mail. 6 00:00:28,740 --> 00:00:32,880 I just open my mailbox, rather than having to check all mailboxes. 7 00:00:32,880 --> 00:00:37,060 Once again, there's a big difference, though, in the way humans and computers behave. 8 00:00:37,060 --> 00:00:40,700 The post person wouldn't try to force a parcel into my mailbox, 9 00:00:40,700 --> 00:00:43,600 because the mailbox slot's only big enough to take letters, 10 00:00:43,600 --> 00:00:47,420 and things wouldn't go well if we tried to squeeze a large parcel in there. 11 00:00:47,420 --> 00:00:49,460 But computers don't think like that, 12 00:00:49,460 --> 00:00:55,000 so we have to tell them what kind of thing we can store in each of the variables that we use. 13 00:00:55,000 --> 00:00:58,000 So let's see how that works with an example. 14 00:00:58,000 --> 00:01:04,040 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 15 00:01:04,040 --> 00:01:10,380 var space tim : space String with a capital S 16 00:01:10,380 --> 00:01:15,140 is equal to, in double quotes, Tim Buchalka. 17 00:01:15,140 --> 00:01:21,300 Then on the line after that I'm going to type println left parenthesis, tim, right parenthesis. 18 00:01:21,300 --> 00:01:24,680 So here what I've done, is created a variable called Tim, 19 00:01:24,680 --> 00:01:29,140 and I've put the text, Tim Buchalka, in the area of memory that's now labelled Tim. 20 00:01:29,140 --> 00:01:32,960 We've told the computer that this area of memory can hold string values, 21 00:01:32,960 --> 00:01:35,480 So it's fine to store text there. 22 00:01:35,480 --> 00:01:40,680 Now string, by the way, is the name given to types of variables that can store text, 23 00:01:40,680 --> 00:01:44,520 and on the next line, line six, we're printing out Tim. 24 00:01:44,520 --> 00:01:47,980 But instead of printing the three characters, T, I and M, 25 00:01:47,980 --> 00:01:53,720 the computer goes off to the area of memory that we've labelled Tim and gets the contents. 26 00:01:53,720 --> 00:01:58,420 It then prints those contents, not the name that we've given to that bit of memory. 27 00:01:58,420 --> 00:02:02,540 So let's run the program to confirm that, 28 00:02:02,540 --> 00:02:04,920 and you can see that it prints out my full name. 29 00:02:04,920 --> 00:02:08,880 So on line five we've declared the variable Tim. 30 00:02:08,880 --> 00:02:13,480 When we declare a variable, we tell the computer what type of information it can store. 31 00:02:13,480 --> 00:02:18,780 In this case, it can store string objects, and we then give the variable a name, Tim. 32 00:02:18,780 --> 00:02:21,900 And when we ran the program it printed my name at the end. 33 00:02:21,900 --> 00:02:27,340 Often you'll keep the variable a value at the same time, which we've done here by using equals, 34 00:02:27,340 --> 00:02:30,120 then the string that we want to assign to the variable, 35 00:02:30,120 --> 00:02:33,260 but you can also assign the value later, if you want. 36 00:02:33,260 --> 00:02:39,620 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, 37 00:02:39,620 --> 00:02:48,000 and I'm going to type Tim on line 6, equals Tim Buchalka. Now when I run that, 38 00:02:48,000 --> 00:02:49,920 it does exactly the same thing. 39 00:02:49,920 --> 00:02:56,160 The important thing is that the variable Tim must be declared before we assign a value to it. 40 00:02:56,160 --> 00:03:00,600 Until we declare the variable, the computer doesn't know what kind of information it can store, 41 00:03:00,600 --> 00:03:03,440 and also hasn't allocated any memory for it. 42 00:03:03,440 --> 00:03:08,600 It's a bit like the post person trying to deliver a letter, but there's no mailbox with my name on it. 43 00:03:08,600 --> 00:03:11,420 Being human, they can make decisions for themselves. 44 00:03:11,420 --> 00:03:16,000 For example, they might ask the neighbours if anyone called Tim has moved in recently. 45 00:03:16,000 --> 00:03:21,200 The computer won't make any decisions that you haven't programmed it to make, so you've just get an error. 46 00:03:21,200 --> 00:03:26,240 So let's see what happens. What I'm going to do is delete the declaration on line 5, 47 00:03:26,240 --> 00:03:30,120 or rather, what I'm going to do is not delete it. I'm going to turn it into a comment instead. 48 00:03:30,120 --> 00:03:37,120 I'm gonna do that by putting two forward slashes, and noting that the color has changed, when I did that, to green. 49 00:03:37,120 --> 00:03:41,880 And to see what happens, let's just run the program again, 50 00:03:41,880 --> 00:03:47,040 and you can see we've now got two errors showing; we we've got unresolved reference, 51 00:03:47,040 --> 00:03:52,500 and also, the same error showing again further down. So we've got basically two errors there. 52 00:03:52,500 --> 00:03:57,260 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. 53 00:03:57,260 --> 00:03:59,740 So that's the name associated to this file. 54 00:03:59,740 --> 00:04:06,780 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. 55 00:04:06,780 --> 00:04:11,060 So we're interested in the line number there. So it's telling us, basically there's an error on line six, 56 00:04:11,060 --> 00:04:15,620 and we can see the code, a little arrow pointing to where it thinks this is a problem. 57 00:04:15,620 --> 00:04:18,640 So go there on line six, and also further down on line seven, 58 00:04:18,640 --> 00:04:22,620 and that's because the variable Tim has no longer been defined. 59 00:04:22,620 --> 00:04:26,680 Basically, it's computer speak for I've got no idea what you mean by Tim. 60 00:04:26,680 --> 00:04:29,540 So what's happened here is, we haven't declared the variable Tim, 61 00:04:29,540 --> 00:04:34,140 because we've commented it it out and so Kotlin's got no idea what it's referring to. 62 00:04:34,140 --> 00:04:37,840 And instead of deleting the declaration it turned it into a comment, as I mentioned. 63 00:04:37,840 --> 00:04:42,120 Any lines starting with those two slashes, like the one I did there, is ignored by Kotlin, 64 00:04:42,120 --> 00:04:44,340 and actually most programming languages. 65 00:04:44,340 --> 00:04:47,940 It's a good idea to put comments in your code to explain what your code's doing, 66 00:04:47,940 --> 00:04:53,940 and it's also a good way to temporarily disable lines of code so they aren't executed, like we did here. 67 00:04:53,940 --> 00:04:58,260 Right, so I'm going to go back and uncomment that line again, 68 00:04:58,260 --> 00:05:04,420 noting that the color changes. Run the program again and the errors disappear and we get the output back. 69 00:05:04,420 --> 00:05:10,660 Alright, so I've declared Tim to be of type string, so that means it can only hold text values. 70 00:05:10,660 --> 00:05:14,240 If we try to assign a number to it, it won't work. 71 00:05:14,240 --> 00:05:19,280 So to see what I mean I'm going to delete the double quote Tim Buchalka and change that to a number, 72 00:05:19,280 --> 00:05:24,560 an arbitrary number 32 there, and if I try and run the program again, 73 00:05:24,560 --> 00:05:30,460 we've got an error now, the integer literal does not conform to the expected type string. 74 00:05:30,460 --> 00:05:35,800 Here we're getting confirmation that we can't assign a number to a string variable. Kotlin doesn't allow that. 75 00:05:35,800 --> 00:05:39,960 Now at this point, you might be tempted to get around that by enclosing the number 32 in double quotes 76 00:05:39,960 --> 00:05:45,700 or speech marks. So let's do that. 77 00:05:45,700 --> 00:05:49,120 You can run the program again. 78 00:05:49,120 --> 00:05:53,600 There's no error now, and we're getting the 32 output in the output window, 79 00:05:53,600 --> 00:05:58,120 but we haven't really fooled the computer though. Tim is still storing a string, 80 00:05:58,120 --> 00:06:01,920 it's just made up of the two characters 3 and 2 now. 81 00:06:01,920 --> 00:06:06,380 So it could be used to represent the number of my mailbox, but not my salary, for example. 82 00:06:06,380 --> 00:06:08,300 In other words, we can't do sums with it. 83 00:06:08,300 --> 00:06:14,260 If we want to treat it as a number, we have to effectively declare it to be a numeric type. 84 00:06:14,260 --> 00:06:18,600 So let's have a go at doing that. So I'm going to add that code. 85 00:06:18,600 --> 00:06:25,600 So we'll start on line 9, and we'll start with var space Tim's salary. 86 00:06:25,600 --> 00:06:30,000 That's : space and Int with a capital I is equal to 32. 87 00:06:30,000 --> 00:06:43,220 Next line, var monthly : Int again, with a space, equals timSalary multiplied by four. 88 00:06:43,220 --> 00:06:50,340 The asterisk is the multiplication symbol. Then we're going to print them out, so print ln timSalary. 89 00:06:50,340 --> 00:06:57,760 Then on the next line, println monthly. Alright. 90 00:06:57,760 --> 00:07:02,820 So the most common Kotlin type for storing whole numbers is Int, short for integer. 91 00:07:02,820 --> 00:07:06,680 By declaring Tim's salary, as I have on line 9, to be an int, 92 00:07:06,680 --> 00:07:10,420 it's now treated as a number, and we can then do arithmetic with it. 93 00:07:10,420 --> 00:07:14,780 and so I can multiply it by 4 to get my monthly salary, which I've done on line 10. 94 00:07:14,780 --> 00:07:19,920 So there's a couple of things there. First, we've declared two variables to be of type Int, 95 00:07:19,920 --> 00:07:21,980 which means that I can store whole numbers. 96 00:07:21,980 --> 00:07:26,300 The second thing is that we can use the value of a variable in calculations, 97 00:07:26,300 --> 00:07:30,320 then assign the result of that calculation to another variable. 98 00:07:30,320 --> 00:07:36,280 And we can see that again on line 10, where the code multiplies the variable tim Salary by 4, 99 00:07:36,280 --> 00:07:43,860 then assigns the result to the variable monthly. So when I run the program, 100 00:07:43,860 --> 00:07:47,740 it correctly calculates my monthly salary and prints out both values. 101 00:07:47,740 --> 00:07:53,580 And notice that it also prints the value of the variable tim out first, that's the first 32 in the output. 102 00:07:53,580 --> 00:07:58,260 They look identical on the screen, but remember that one's a string and the others are number. 103 00:07:58,260 --> 00:08:04,860 And to confirm that, if I try to multiply 10 by 4, on line 10, instead of Tim's salary, 104 00:08:04,860 --> 00:08:09,960 we'll get an error, so I'm going to change the variable name to Tim. 105 00:08:09,960 --> 00:08:15,660 So if we run that, we've now got an error, as you can see on the screen there. 106 00:08:15,660 --> 00:08:21,420 So that error's a bit cryptic, None of the following candidates is applicable because of a receiver type mismatch. 107 00:08:21,420 --> 00:08:24,860 What it means is that you can't multiply a string by an integer, 108 00:08:24,860 --> 00:08:29,760 and actually you can't multiply a string by anything, but the computer doesn't do that kind of semantic reasoning. 109 00:08:29,760 --> 00:08:32,840 It just sees a string and an int, so that's what it reports. 110 00:08:32,840 --> 00:08:38,220 So I'm gonna put that back to what it was to get rid of the error, 111 00:08:38,220 --> 00:08:41,740 and also take the opportunity of changing our first definition up here for Tim, 112 00:08:41,740 --> 00:08:46,400 instead of 32, setting that back to what it was, Tim Buchalka. 113 00:08:46,400 --> 00:08:51,800 We'll run the program again, and we've got it working as it should now. 114 00:08:51,800 --> 00:08:53,720 Alright so let's end the video there. 115 00:08:53,720 --> 00:08:57,500 In the next video in this playlist, we're going to talk about variable names, 116 00:08:57,500 --> 00:09:02,040 and why I'm doing things like putting a capital S for the variable name timSalary. 117 00:09:02,040 --> 00:09:05,040 So I'll see you in that next video.