1 00:00:00,000 --> 00:00:02,357 (upbeat music) 2 00:00:02,357 --> 00:00:05,150 (keys clicking) 3 00:00:05,150 --> 00:00:07,650 Okay, so we've looked at declaring variables 4 00:00:07,650 --> 00:00:09,600 and using them in calculations. 5 00:00:09,600 --> 00:00:11,940 We've then looked at using String interpolation 6 00:00:11,940 --> 00:00:13,790 to print out more descriptive messages 7 00:00:13,790 --> 00:00:15,430 with our variables. 8 00:00:15,430 --> 00:00:17,830 Now we've got a couple of things to cover in this video, 9 00:00:17,830 --> 00:00:19,800 and I'll start with why I've used var 10 00:00:19,800 --> 00:00:23,090 for some of the variables and val for others. 11 00:00:23,090 --> 00:00:25,270 And I've actually got an admission to make, 12 00:00:25,270 --> 00:00:26,530 as this code is at the moment 13 00:00:26,530 --> 00:00:28,750 I should've used val everywhere. 14 00:00:28,750 --> 00:00:30,780 But then I wouldn't have had any familiar example code 15 00:00:30,780 --> 00:00:32,590 to use for this video. 16 00:00:32,590 --> 00:00:35,310 Alright, so what does this val and var mean, 17 00:00:35,310 --> 00:00:37,470 and what's the difference between the two? 18 00:00:37,470 --> 00:00:42,200 Well val stands for value, and var is short for variable. 19 00:00:42,200 --> 00:00:44,420 Now, we can get a clue as to what the difference is 20 00:00:44,420 --> 00:00:46,750 by looking at the third warning tick 21 00:00:46,750 --> 00:00:49,080 that Android Studio is showing in the right margin. 22 00:00:49,080 --> 00:00:51,563 So if you come down here and just hover over that, 23 00:00:52,500 --> 00:00:54,937 you can see that it says "variable is never modified 24 00:00:54,937 --> 00:00:58,390 "and can be declared immutable using val." 25 00:00:58,390 --> 00:01:01,320 Now immutable here means that it can't be changed, 26 00:01:01,320 --> 00:01:03,640 which means that once it's been given a value, 27 00:01:03,640 --> 00:01:05,900 that value can't change. 28 00:01:05,900 --> 00:01:07,560 Now, that may seem like an odd thing 29 00:01:07,560 --> 00:01:09,400 to want to do to a variable. 30 00:01:09,400 --> 00:01:11,410 After all, they're called variables, 31 00:01:11,410 --> 00:01:14,290 which implies that their values will change. 32 00:01:14,290 --> 00:01:16,670 And that's true, it's often necessary to assign 33 00:01:16,670 --> 00:01:18,770 different values to a variable. 34 00:01:18,770 --> 00:01:21,373 In that case, they'd have to be declared using var 35 00:01:23,000 --> 00:01:24,620 rather than val. 36 00:01:24,620 --> 00:01:27,600 Very often, though, you'll assign a value to a variable 37 00:01:27,600 --> 00:01:29,090 and never change it. 38 00:01:29,090 --> 00:01:32,900 Now, that might sound surprising, but it's extremely common. 39 00:01:32,900 --> 00:01:34,870 You'll see examples of that when we look at functions 40 00:01:34,870 --> 00:01:37,610 and classes later in this section of the course, 41 00:01:37,610 --> 00:01:39,160 but as an example of this, 42 00:01:39,160 --> 00:01:44,160 look at the declaration here on line 5 for the tim variable. 43 00:01:44,570 --> 00:01:47,410 Now I could probably imagine an extreme set of circumstances 44 00:01:47,410 --> 00:01:48,980 that would result in my changing my name 45 00:01:48,980 --> 00:01:51,830 and going on the run and going into hiding. 46 00:01:51,830 --> 00:01:54,980 It's possible, perhaps, but I think I can safely guarantee 47 00:01:54,980 --> 00:01:57,270 that my name will remain Tim Buchalka, 48 00:01:57,270 --> 00:01:59,450 I won't change in the time I'm recording these videos, 49 00:01:59,450 --> 00:02:00,700 at least, if ever. 50 00:02:00,700 --> 00:02:03,240 So the point is that there's very little point 51 00:02:03,240 --> 00:02:05,860 storing my name in a var variable. 52 00:02:05,860 --> 00:02:08,770 Once it's stored, it's not going to change. 53 00:02:08,770 --> 00:02:09,840 Alright, so that's fair enough, 54 00:02:09,840 --> 00:02:11,020 but what's the big deal? 55 00:02:11,020 --> 00:02:13,060 After all, we just have to avoid changing it, 56 00:02:13,060 --> 00:02:14,860 and everything's fine, right? 57 00:02:14,860 --> 00:02:17,250 Now this bit's going to be a little bit contrived, 58 00:02:17,250 --> 00:02:18,810 and we haven't got a lot of code here, 59 00:02:18,810 --> 00:02:21,580 so it's gonna be obvious where I'm about to go wrong. 60 00:02:21,580 --> 00:02:24,180 But remember that a real programme will have hundreds 61 00:02:24,180 --> 00:02:25,990 or thousands of lines of codes 62 00:02:25,990 --> 00:02:29,010 split over loads of different files and classes. 63 00:02:29,010 --> 00:02:30,820 So what I'm gonna do here is start by adding 64 00:02:30,820 --> 00:02:32,400 another variable, Jim, 65 00:02:32,400 --> 00:02:35,890 and I'm gonna do that above tim on line 5. 66 00:02:35,890 --> 00:02:40,890 So V-A-R, jim, J-I-M, colon, String. 67 00:02:43,810 --> 00:02:44,643 Alright, so now that we've done that, 68 00:02:44,643 --> 00:02:46,780 we're gonna go down to the code 69 00:02:46,780 --> 00:02:47,630 right down to the bottom here, 70 00:02:47,630 --> 00:02:49,350 we're gonna give jim a value. 71 00:02:49,350 --> 00:02:50,900 So right after the last println 72 00:02:52,250 --> 00:02:57,250 we're going to put tim equals James T Kirk. 73 00:02:59,970 --> 00:03:01,320 Now when I run the programme, 74 00:03:04,470 --> 00:03:06,590 you can see that it says "My name is James T Kirk," 75 00:03:06,590 --> 00:03:08,350 so I'm no longer Tim Buchalka. 76 00:03:08,350 --> 00:03:11,170 I've become the captain of the enterprise instead. 77 00:03:11,170 --> 00:03:13,520 The rest I've said, this is a bit contrived, 78 00:03:13,520 --> 00:03:16,940 but errors like this are surprisingly easy to make 79 00:03:16,940 --> 00:03:18,760 when you're dealing with hundred of variables 80 00:03:18,760 --> 00:03:21,440 and thousands of lines of programming code. 81 00:03:21,440 --> 00:03:24,370 Now, we can prevent bugs like that from creeping in 82 00:03:24,370 --> 00:03:26,830 by declaring variables as val 83 00:03:26,830 --> 00:03:29,020 when we don't expect them to change. 84 00:03:29,020 --> 00:03:30,850 So I want to go up here into line 6 85 00:03:30,850 --> 00:03:33,023 and change the var to val. 86 00:03:35,142 --> 00:03:35,980 And scroll down and have a look, 87 00:03:35,980 --> 00:03:38,570 the code now would no longer compile a run. 88 00:03:38,570 --> 00:03:39,970 We're actually getting a warning here, 89 00:03:39,970 --> 00:03:43,370 an error rather saying val cannot be reassigned. 90 00:03:43,370 --> 00:03:44,630 So it's an error showing that 91 00:03:44,630 --> 00:03:46,090 when I incorrectly try to assign 92 00:03:46,090 --> 00:03:48,760 a new value to tim at the end of the code. 93 00:03:48,760 --> 00:03:51,600 So the Kotlin compiler is catching the bug for us 94 00:03:51,600 --> 00:03:53,770 and not allowing the code to compile. 95 00:03:53,770 --> 00:03:56,860 So as a general rule, always use val 96 00:03:56,860 --> 00:03:58,670 unless you're sure that you're going to change 97 00:03:58,670 --> 00:04:00,720 the value of a variable. 98 00:04:00,720 --> 00:04:01,980 Now if you find that you do need 99 00:04:01,980 --> 00:04:03,770 to assign a new value to a val, 100 00:04:03,770 --> 00:04:04,870 you can always go back and make it 101 00:04:04,870 --> 00:04:06,710 a var instead at that point. 102 00:04:06,710 --> 00:04:08,900 Alright, so I'm going to correct that rogue line 103 00:04:08,900 --> 00:04:11,513 so that we assign to jim instead of tim. 104 00:04:14,120 --> 00:04:16,100 And let's also print something. 105 00:04:16,100 --> 00:04:18,632 We'll actually replace line 32. 106 00:04:19,540 --> 00:04:22,007 What I'll do is come down here 107 00:04:22,007 --> 00:04:25,367 and on line 33, we'll change that to put 108 00:04:25,367 --> 00:04:28,127 "jim is dollar jim." 109 00:04:29,530 --> 00:04:30,830 Okay, we'll run that code. 110 00:04:32,550 --> 00:04:34,880 Quickly now, showing "My name is Tim Buchalka" 111 00:04:34,880 --> 00:04:37,500 and the jim variable is correctly assigned 112 00:04:37,500 --> 00:04:40,000 to the value of James T Kirk. 113 00:04:40,000 --> 00:04:42,630 Alright, so Android Studio is helping to identify 114 00:04:42,630 --> 00:04:44,670 variables that don't change, 115 00:04:44,670 --> 00:04:47,280 and all uses of var are actually highlighted. 116 00:04:47,280 --> 00:04:50,160 We can actually start by clicking on var on line 5, 117 00:04:50,160 --> 00:04:52,030 over here, and we get the tool tip 118 00:04:52,030 --> 00:04:54,147 suggesting it can be a val as you can see here. 119 00:04:54,147 --> 00:04:55,500 "Variable is never modified 120 00:04:55,500 --> 00:04:57,780 and can be declared immutable using val." 121 00:04:57,780 --> 00:05:01,560 But even better, though, if I double click it, 122 00:05:01,560 --> 00:05:03,030 we get this little light bulb up here 123 00:05:03,030 --> 00:05:04,333 in the left-hand side, 124 00:05:05,280 --> 00:05:06,510 and we can come down here, we can click on 125 00:05:06,510 --> 00:05:09,940 make variable immutable, and that changes that, 126 00:05:09,940 --> 00:05:11,600 as you can see, to a val now. 127 00:05:11,600 --> 00:05:14,270 And obviously, that warning then disappears. 128 00:05:14,270 --> 00:05:17,320 Right, so let's do the same for the other vars. 129 00:05:17,320 --> 00:05:20,490 So I come down here, we can declare that, 130 00:05:20,490 --> 00:05:23,160 by double clicking it, make variable immutable. 131 00:05:23,160 --> 00:05:26,120 Same for timsMonthlySalary, double click the var, 132 00:05:26,120 --> 00:05:28,780 change that to make variable immutable. 133 00:05:28,780 --> 00:05:31,400 We can come down here to fruit, double click, 134 00:05:31,400 --> 00:05:34,250 you can also make that variable immutable as you can see. 135 00:05:35,270 --> 00:05:37,820 And that's all the vars now converted to val. 136 00:05:37,820 --> 00:05:39,220 In this case we can do that 137 00:05:39,220 --> 00:05:42,120 because we're only assigning a value to them once. 138 00:05:42,120 --> 00:05:44,130 Alright, so let's now have a look at some of the warnings 139 00:05:44,130 --> 00:05:46,870 that are still showing in the right hand margin. 140 00:05:46,870 --> 00:05:48,180 Now the first one, if you look carefully, 141 00:05:48,180 --> 00:05:50,210 it looks like it's changed colour slightly. 142 00:05:50,210 --> 00:05:52,430 It's now a suggestion where as before 143 00:05:52,430 --> 00:05:53,990 it was showing us a warning. 144 00:05:53,990 --> 00:05:56,600 Now suggestions aren't as important as warnings, 145 00:05:56,600 --> 00:05:58,130 but your code will be more readable 146 00:05:58,130 --> 00:05:59,610 if you take note of them. 147 00:05:59,610 --> 00:06:02,920 So this one here is saying "Can be joined with assignment." 148 00:06:02,920 --> 00:06:06,500 And by the way, clicking on one of these warnings 149 00:06:06,500 --> 00:06:09,270 will take you to the line of code that's applicable 150 00:06:09,270 --> 00:06:11,390 for that particular warning. 151 00:06:11,390 --> 00:06:14,670 Now we can come over here, double click it again, 152 00:06:14,670 --> 00:06:18,650 and we can come down and select the light bulb, 153 00:06:18,650 --> 00:06:21,300 and we'll click on "Join declaration and assignment." 154 00:06:22,280 --> 00:06:23,720 And you can see that that's pretty impressive. 155 00:06:23,720 --> 00:06:26,900 It's pulled the assignment of the jim variable 156 00:06:26,900 --> 00:06:29,950 from the end of the code and added it to our declaration. 157 00:06:29,950 --> 00:06:33,470 So jim's now initialised at the point where it's declared, 158 00:06:33,470 --> 00:06:35,410 and that's good programming practise. 159 00:06:35,410 --> 00:06:37,760 So I can do the same for tim on the next line, 160 00:06:37,760 --> 00:06:40,670 just by double clicking on it and selecting the light bulb, 161 00:06:40,670 --> 00:06:43,800 clicking on "Join declaration and assignment." 162 00:06:43,800 --> 00:06:46,177 Alright, so let's look at some of the other warnings now. 163 00:06:46,177 --> 00:06:48,460 "Explicit given type is redundant," 164 00:06:48,460 --> 00:06:49,960 and they're actually all the same 165 00:06:49,960 --> 00:06:51,540 at this point forward, now. 166 00:06:51,540 --> 00:06:53,167 All these warnings are all the same now, 167 00:06:53,167 --> 00:06:56,410 "Explicit given type is redundant here." 168 00:06:56,410 --> 00:06:58,810 Now, I said that you have to tell Kotlin 169 00:06:58,810 --> 00:07:01,790 what type of value each variable is gonna be given, 170 00:07:01,790 --> 00:07:03,780 but that's not strictly true. 171 00:07:03,780 --> 00:07:06,460 Now, every variable has to have a type, 172 00:07:06,460 --> 00:07:07,700 and once you've made it a String, say, 173 00:07:07,700 --> 00:07:09,880 you can't go assigning a number to it. 174 00:07:09,880 --> 00:07:13,170 But we don't have to explicitly tell Kotlin the type. 175 00:07:13,170 --> 00:07:16,100 Kotlin's very clever and can work out 176 00:07:16,100 --> 00:07:18,750 what the type should be most of the time. 177 00:07:18,750 --> 00:07:20,800 Now the first warning is here on line 5, 178 00:07:20,800 --> 00:07:23,200 I click on over here where we assign 179 00:07:23,200 --> 00:07:25,250 a String the the variable jim. 180 00:07:25,250 --> 00:07:27,610 Now if we're making jim equal to the String, 181 00:07:27,610 --> 00:07:30,380 then double quotes and whatever the name of the String is, 182 00:07:30,380 --> 00:07:31,530 then jim's obviously a string. 183 00:07:31,530 --> 00:07:32,710 So in other words, if the value 184 00:07:32,710 --> 00:07:34,440 is surrounded by double quotes, 185 00:07:34,440 --> 00:07:37,220 quite clearly, that's a string. 186 00:07:37,220 --> 00:07:38,820 And that's what the warning's saying here. 187 00:07:38,820 --> 00:07:41,950 Kotlin can work out that jim is of type String, 188 00:07:41,950 --> 00:07:44,760 so telling it that it's a string is redundant. 189 00:07:44,760 --> 00:07:48,060 And again, what we can do here is just to double click, 190 00:07:48,060 --> 00:07:50,287 click on the light bulb, and we can click on 191 00:07:50,287 --> 00:07:54,155 "Remove explicit type specification." 192 00:07:54,155 --> 00:07:55,750 And now that I've done that you can see it just says 193 00:07:55,750 --> 00:07:59,280 val jim equal double quotes James T Kirk 194 00:07:59,280 --> 00:08:01,030 and then another double quote. 195 00:08:01,030 --> 00:08:02,910 So result, I think you'd agree that line 5 196 00:08:02,910 --> 00:08:06,130 now is a lot easier to read than line 6, 197 00:08:06,130 --> 00:08:07,447 and we've removed that redundant type 198 00:08:07,447 --> 00:08:10,580 and we've actually removed that warning as well. 199 00:08:10,580 --> 00:08:13,340 Now when the code's compiled, the Kotlan compiler 200 00:08:13,340 --> 00:08:16,230 will infer the type that jim should be. 201 00:08:16,230 --> 00:08:18,510 Now, you'll come across the term "type inference" 202 00:08:18,510 --> 00:08:21,280 in the documentation, and that's what it means. 203 00:08:21,280 --> 00:08:23,880 Kotlan can work out what type something is. 204 00:08:23,880 --> 00:08:25,620 Right, so I'm gonna use the light bulb now 205 00:08:25,620 --> 00:08:29,788 to actually remove the remaining redundant type specifiers. 206 00:08:29,788 --> 00:08:32,027 So I'm gonna start with the next line, 207 00:08:32,027 --> 00:08:33,722 I'm gonna double click on that. 208 00:08:35,159 --> 00:08:39,727 I'm gonna change tim, remove explicit type specification. 209 00:08:39,727 --> 00:08:41,732 And the next one, timsWeeklySalary. 210 00:08:42,630 --> 00:08:45,570 Gonna remove explicit type specification. 211 00:08:45,570 --> 00:08:47,103 Next one, timsMonthlySalary, 212 00:08:49,070 --> 00:08:51,610 remove explicit type specification. 213 00:08:51,610 --> 00:08:54,320 Now that can also be inferred, but there's no warning, 214 00:08:54,320 --> 00:08:56,050 but the light bulb did appear when I clicked on 215 00:08:56,050 --> 00:08:57,336 the variable while type, 216 00:08:57,336 --> 00:08:59,057 and also appeared by the white, 217 00:08:59,057 --> 00:09:02,970 and you can, by double clicking on the extra variable top 218 00:09:02,970 --> 00:09:04,030 that'll produce a light bulb 219 00:09:04,030 --> 00:09:06,280 or make the light bulb appear as well. 220 00:09:06,280 --> 00:09:08,380 I can click on "Remove explicit type specification 221 00:09:08,380 --> 00:09:09,960 for that as well. 222 00:09:09,960 --> 00:09:13,023 And continuing on down here, I can double click on apples. 223 00:09:13,950 --> 00:09:15,300 Remove explicit type. 224 00:09:15,300 --> 00:09:18,840 Oranges, remove explicit type. 225 00:09:18,840 --> 00:09:22,360 Fruit, remove explicit type. 226 00:09:22,360 --> 00:09:25,900 Weeks, remove explicit type. 227 00:09:25,900 --> 00:09:28,773 Years, remove explicit type. 228 00:09:31,120 --> 00:09:34,220 Okay, so that's basically all the changes there. 229 00:09:34,220 --> 00:09:36,700 Now that's actually removed all the warnings, 230 00:09:36,700 --> 00:09:39,360 and our code's more readable without that clutter. 231 00:09:39,360 --> 00:09:40,380 Notice now that I've done that, 232 00:09:40,380 --> 00:09:43,397 in the the top right-hand corner here there's a green tick. 233 00:09:43,397 --> 00:09:45,440 Now that's telling us that there's no other warnings. 234 00:09:45,440 --> 00:09:47,350 We've got these other things here, 235 00:09:47,350 --> 00:09:49,700 which aren't warnings as such. 236 00:09:49,700 --> 00:09:52,860 So this is what you should be aiming for in your code. 237 00:09:52,860 --> 00:09:54,160 A green tick to indicate that 238 00:09:54,160 --> 00:09:56,840 there's no outstanding warnings or suggestions 239 00:09:56,840 --> 00:09:58,920 and obviously, no errors. 240 00:09:58,920 --> 00:10:00,550 Now it's always worth checking out these 241 00:10:00,550 --> 00:10:03,160 warnings and suggestions, as they'll often help you 242 00:10:03,160 --> 00:10:06,320 to write more idiomatic Kotlin code. 243 00:10:06,320 --> 00:10:08,590 You can ignore them, but paying attention to them 244 00:10:08,590 --> 00:10:11,053 and acting on them can improve your code. 245 00:10:11,940 --> 00:10:13,970 Alright, so I'm going to stop this video here. 246 00:10:13,970 --> 00:10:15,150 In the next one, we're going to look at 247 00:10:15,150 --> 00:10:17,410 how our code can make decisions. 248 00:10:17,410 --> 00:10:18,713 See you in the next video.