1 00:00:04,950 --> 00:00:07,379 Alright so we're all set up now so 2 00:00:07,379 --> 00:00:10,170 it's time to start learning Kotlin, 3 00:00:10,170 --> 00:00:12,180 so start Android studio and then open the 4 00:00:12,180 --> 00:00:14,969 Kotlin tutorial project so I'm gonna 5 00:00:14,969 --> 00:00:16,470 open up this by clicking on Kotlin 6 00:00:16,470 --> 00:00:19,320 tutorial and just a reminder to ignore 7 00:00:19,320 --> 00:00:22,140 the outdated Kotlin runtime or messages 8 00:00:22,140 --> 00:00:23,460 of that nature for this particular 9 00:00:23,460 --> 00:00:26,009 project because we've had to hack things 10 00:00:26,009 --> 00:00:28,050 together to get it to work, so if you 11 00:00:28,050 --> 00:00:29,219 do that you'll find that things won't 12 00:00:29,219 --> 00:00:32,790 work properly. Alright now open up the 13 00:00:32,790 --> 00:00:34,770 main dot kt file by double clicking it 14 00:00:34,770 --> 00:00:36,000 if it's not already showing on the 15 00:00:36,000 --> 00:00:38,100 right-hand side of the screen, in my case 16 00:00:38,100 --> 00:00:40,260 it was already open and by the way if 17 00:00:40,260 --> 00:00:41,790 you want to close it at any time you can 18 00:00:41,790 --> 00:00:42,870 come over here and click on the X to 19 00:00:42,870 --> 00:00:44,880 close it and then we'll click it to open 20 00:00:44,880 --> 00:00:46,950 it again so that's what you do when you 21 00:00:46,950 --> 00:00:48,060 want to work with different files you 22 00:00:48,060 --> 00:00:49,380 can just choose which ones to appear in 23 00:00:49,380 --> 00:00:51,060 the tab list above the editor window 24 00:00:51,060 --> 00:00:52,980 here and you can have multiple ones on 25 00:00:52,980 --> 00:00:55,079 that list and click on to select the 26 00:00:55,079 --> 00:00:58,350 different ones now unlike Java you don't 27 00:00:58,350 --> 00:01:00,600 need to create a class to hold your 28 00:01:00,600 --> 00:01:03,149 Kotlin code and if I keep referring to 29 00:01:03,149 --> 00:01:05,670 Java in this tutorial things are going 30 00:01:05,670 --> 00:01:07,320 to get very confusing for students who 31 00:01:07,320 --> 00:01:08,759 don't understand Java. 32 00:01:08,759 --> 00:01:10,859 Now there's one example I've just talked 33 00:01:10,859 --> 00:01:12,149 about classes and we haven't even 34 00:01:12,149 --> 00:01:14,639 discussed what they are yet so for most 35 00:01:14,639 --> 00:01:17,340 of this tutorial section I'm not going 36 00:01:17,340 --> 00:01:19,590 to keep comparing things to Java I won't 37 00:01:19,590 --> 00:01:21,270 promise not to mention Java now and then 38 00:01:21,270 --> 00:01:23,759 but I won't do it very often we will 39 00:01:23,759 --> 00:01:25,499 start by looking at that simple function 40 00:01:25,499 --> 00:01:27,689 on-screen here that just prints out 41 00:01:27,689 --> 00:01:29,249 hello world that you saw running in the 42 00:01:29,249 --> 00:01:31,499 previous video. Now that looks a bit 43 00:01:31,499 --> 00:01:33,359 horrible but this particular function 44 00:01:33,359 --> 00:01:36,270 has to follow that format now the reason 45 00:01:36,270 --> 00:01:38,399 is that the computer needs to know which 46 00:01:38,399 --> 00:01:40,439 function it should start with when you 47 00:01:40,439 --> 00:01:42,959 run your program so you have to start 48 00:01:42,959 --> 00:01:45,270 with a function called main that has a 49 00:01:45,270 --> 00:01:47,369 declaration exactly like we've got here 50 00:01:47,369 --> 00:01:49,709 on line one now things are a bit 51 00:01:49,709 --> 00:01:51,509 different when creating an Android app 52 00:01:51,509 --> 00:01:54,240 rather than a Kotlin program but at the 53 00:01:54,240 --> 00:01:56,520 moment we're learning Kotlin so we have 54 00:01:56,520 --> 00:01:58,889 to use a main function in this form now 55 00:01:58,889 --> 00:02:02,099 the keyword fund defines a function and 56 00:02:02,099 --> 00:02:05,099 our functions called main you everything 57 00:02:05,099 --> 00:02:06,599 between the opening and closing braces 58 00:02:06,599 --> 00:02:09,380 as these here at the end of line 1 and 59 00:02:09,380 --> 00:02:12,900 the style of line 3 that's part of this 60 00:02:12,900 --> 00:02:15,420 function now inside the function is the 61 00:02:15,420 --> 00:02:18,540 code that's executed when our program runs 62 00:02:18,540 --> 00:02:20,250 make sure you put your coat in between 63 00:02:20,250 --> 00:02:22,080 these two curly braces so you can see 64 00:02:22,080 --> 00:02:24,180 here on line one on line three when you 65 00:02:24,180 --> 00:02:25,980 want it to be executed as part of this 66 00:02:25,980 --> 00:02:28,290 function so this function prints out the 67 00:02:28,290 --> 00:02:30,390 message helloworld which is what this 68 00:02:30,390 --> 00:02:32,519 print line statement here on line two 69 00:02:32,519 --> 00:02:35,700 actually does. Now Kotlin programs are 70 00:02:35,700 --> 00:02:37,409 normally a lot more complex than this 71 00:02:37,409 --> 00:02:39,870 they contain a lot more classes and 72 00:02:39,870 --> 00:02:42,329 functions and can also repeat bits of 73 00:02:42,329 --> 00:02:44,459 code and so forth but this is a very 74 00:02:44,459 --> 00:02:47,400 basic Kotlin program so what our 75 00:02:47,400 --> 00:02:48,810 function is doing is calling another 76 00:02:48,810 --> 00:02:51,359 function called print 'ln and that's 77 00:02:51,359 --> 00:02:53,129 short for print and start a new line and 78 00:02:53,129 --> 00:02:55,859 the functions part of the Kotlin library 79 00:02:55,859 --> 00:02:58,109 so that just means it comes with Collin 80 00:02:58,109 --> 00:03:00,720 to give us a way to print things so we 81 00:03:00,720 --> 00:03:02,700 tell it what to print by putting what's 82 00:03:02,700 --> 00:03:04,650 called an argument inside these 83 00:03:04,650 --> 00:03:06,629 parentheses as you can see here on line 84 00:03:06,629 --> 00:03:09,989 two so that's the argument that's in 85 00:03:09,989 --> 00:03:11,849 between those two in between the 86 00:03:11,849 --> 00:03:13,620 parentheses or inside the parentheses 87 00:03:13,620 --> 00:03:15,959 and here the argument is the text hello 88 00:03:15,959 --> 00:03:18,389 world in double quotes and by the way 89 00:03:18,389 --> 00:03:20,579 because we're dealing with text rather 90 00:03:20,579 --> 00:03:23,220 than numbers we enclose hello world in 91 00:03:23,220 --> 00:03:24,959 speech marks all these double quotes as 92 00:03:24,959 --> 00:03:27,120 we call them in Australia so our main 93 00:03:27,120 --> 00:03:29,370 function uses another function called 94 00:03:29,370 --> 00:03:31,980 print 'ln to print the text that we've 95 00:03:31,980 --> 00:03:35,340 given it. Now arguments are values that 96 00:03:35,340 --> 00:03:37,709 you provide to functions to give them 97 00:03:37,709 --> 00:03:39,209 the information that you want them to 98 00:03:39,209 --> 00:03:41,819 work with so the print line function 99 00:03:41,819 --> 00:03:44,459 needs to know what to print and we tell 100 00:03:44,459 --> 00:03:45,769 it by giving it a string of characters 101 00:03:45,769 --> 00:03:48,180 enclosed in these speech marks or double 102 00:03:48,180 --> 00:03:50,459 quotes so let's just run the program 103 00:03:50,459 --> 00:03:52,139 again to see if it's doing what we 104 00:03:52,139 --> 00:03:54,629 expect so what we can do we can also 105 00:03:54,629 --> 00:03:57,900 right-click in here within the editor 106 00:03:57,900 --> 00:04:00,419 window to get the context menu right 107 00:04:00,419 --> 00:04:02,069 click it and I'm going to select main 108 00:04:02,069 --> 00:04:06,150 Katie if I select that you can see down 109 00:04:06,150 --> 00:04:07,620 the bottom bottom left-hand corner we've 110 00:04:07,620 --> 00:04:10,560 got holo world showing on the screen now 111 00:04:10,560 --> 00:04:12,000 the console should have opened up 112 00:04:12,000 --> 00:04:14,400 automatically but if it doesn't you may 113 00:04:14,400 --> 00:04:16,409 need to click on this run tab at the 114 00:04:16,409 --> 00:04:18,238 bottom of the screen over here and say 115 00:04:18,238 --> 00:04:20,630 you've got to turn it off and turn it on again 116 00:04:20,630 --> 00:04:23,159 so if it's open it's it'll close when I 117 00:04:23,159 --> 00:04:24,300 click it if it's closed 118 00:04:24,300 --> 00:04:25,530 I'll open it'll open when I actually 119 00:04:25,530 --> 00:04:27,419 click it again alright so there's our 120 00:04:27,419 --> 00:04:30,240 holo world text printed out now when a 121 00:04:30,240 --> 00:04:32,400 program runs without any errors 122 00:04:32,400 --> 00:04:33,720 we'll see that the message at the end 123 00:04:33,720 --> 00:04:37,020 says price has finished with exit code 124 00:04:37,020 --> 00:04:39,600 zero. Now normally an extra code other 125 00:04:39,600 --> 00:04:41,669 than zero means that something's gone 126 00:04:41,669 --> 00:04:43,650 wrong but you'll normally get an error 127 00:04:43,650 --> 00:04:45,389 message as well so you'll know if that 128 00:04:45,389 --> 00:04:45,990 hasn't worked. 129 00:04:45,990 --> 00:04:48,240 So let's actually print something else 130 00:04:48,240 --> 00:04:52,199 out so they'll come down here and type 131 00:04:52,199 --> 00:04:58,260 in print 'ln they're going to type my 132 00:04:58,260 --> 00:05:06,090 first Kotlin program and we've got this 133 00:05:06,090 --> 00:05:07,919 red squiggle at the end of the line as 134 00:05:07,919 --> 00:05:10,320 you can see over here and over to the 135 00:05:10,320 --> 00:05:12,539 right-hand margin is a red dash over 136 00:05:12,539 --> 00:05:15,030 here. Now Edward studio does that to 137 00:05:15,030 --> 00:05:17,789 indicate errors and warnings we'll be 138 00:05:17,789 --> 00:05:19,229 seeing some warnings later they'll 139 00:05:19,229 --> 00:05:20,430 appear in a different color 140 00:05:20,430 --> 00:05:22,740 normally orange or amber and you can 141 00:05:22,740 --> 00:05:25,020 choose to ignore those it's a good idea 142 00:05:25,020 --> 00:05:26,760 that one to at least read them though 143 00:05:26,760 --> 00:05:28,680 because they'll often highlight problems 144 00:05:28,680 --> 00:05:31,139 with your code the warnings won't stop 145 00:05:31,139 --> 00:05:33,180 the code from running but they could 146 00:05:33,180 --> 00:05:34,650 mean that it won't do quite what you 147 00:05:34,650 --> 00:05:37,590 expected but these red arrows Oh can't 148 00:05:37,590 --> 00:05:39,900 be ignored the code just won't run when 149 00:05:39,900 --> 00:05:42,150 you've got errors like this so to see 150 00:05:42,150 --> 00:05:44,490 what the actual error is you can hover 151 00:05:44,490 --> 00:05:46,289 the mouse either over the school over 152 00:05:46,289 --> 00:05:49,820 here or over here in the right margin 153 00:05:49,820 --> 00:05:53,130 the arrow is just saying expecting and 154 00:05:53,130 --> 00:05:55,770 then in single quotes our right 155 00:05:55,770 --> 00:05:57,780 parenthesis so it's just telling us that 156 00:05:57,780 --> 00:05:59,580 the line must end with the closing 157 00:05:59,580 --> 00:06:02,370 parenthesis now computers are very picky 158 00:06:02,370 --> 00:06:04,560 and it's important that your code 159 00:06:04,560 --> 00:06:06,900 follows the syntax of the programming 160 00:06:06,900 --> 00:06:09,419 language exactly you'll pick up the 161 00:06:09,419 --> 00:06:11,190 rules by reading and writing code so 162 00:06:11,190 --> 00:06:12,840 don't think that you have to remember a 163 00:06:12,840 --> 00:06:14,820 whole lot of complicated grammar before 164 00:06:14,820 --> 00:06:16,919 you can program if you do make mistakes 165 00:06:16,919 --> 00:06:18,570 which you will because it's a natural 166 00:06:18,570 --> 00:06:20,940 part of learning to program then Android 167 00:06:20,940 --> 00:06:23,099 studio or the Kotlin compiler will give 168 00:06:23,099 --> 00:06:26,130 you errors to indicate what's wrong now 169 00:06:26,130 --> 00:06:27,180 there's nothing actually wrong with 170 00:06:27,180 --> 00:06:28,889 getting errors it's a natural part of 171 00:06:28,889 --> 00:06:30,479 learning how to program you know I wish 172 00:06:30,479 --> 00:06:31,979 I could claim that I never get errors in 173 00:06:31,979 --> 00:06:34,440 my code but even after over thirty years 174 00:06:34,440 --> 00:06:36,900 of programming I still make mistakes so 175 00:06:36,900 --> 00:06:38,340 don't worry the more mistakes you make 176 00:06:38,340 --> 00:06:40,050 though that probably means you're 177 00:06:40,050 --> 00:06:42,510 learning faster so unlike human 178 00:06:42,510 --> 00:06:45,870 languages computer languages are very precise 179 00:06:45,870 --> 00:06:47,670 you have to stick to the syntax of the 180 00:06:47,670 --> 00:06:49,770 language and if you think about it even 181 00:06:49,770 --> 00:06:51,750 human languages need correct punctuation 182 00:06:51,750 --> 00:06:54,240 though being intelligent we can often 183 00:06:54,240 --> 00:06:56,100 work out what so really meant if the 184 00:06:56,100 --> 00:06:57,830 punctuation isn't quite right but 185 00:06:57,830 --> 00:07:00,780 punctuation is still important have a 186 00:07:00,780 --> 00:07:03,840 look at this slide now here's exactly 187 00:07:03,840 --> 00:07:06,750 the same text but punctuated in two 188 00:07:06,750 --> 00:07:08,970 different ways and if you haven't read 189 00:07:08,970 --> 00:07:11,460 of both of those you'll see that there's 190 00:07:11,460 --> 00:07:13,740 a big difference just by adding 191 00:07:13,740 --> 00:07:16,980 punctuation in appropriate places so if 192 00:07:16,980 --> 00:07:18,630 you do find yourself getting frustrated 193 00:07:18,630 --> 00:07:20,760 remember that that it's only a computer 194 00:07:20,760 --> 00:07:22,980 he can't think for itself and you have 195 00:07:22,980 --> 00:07:25,440 to compensate for that by being very 196 00:07:25,440 --> 00:07:28,440 precise in how you write your code so 197 00:07:28,440 --> 00:07:29,970 that the poor computer can understand 198 00:07:29,970 --> 00:07:32,790 you. Alright so back to Android studio 199 00:07:32,790 --> 00:07:34,470 so it's telling us there's something 200 00:07:34,470 --> 00:07:35,280 wrong here, 201 00:07:35,280 --> 00:07:36,810 so I'm going to add the closing 202 00:07:36,810 --> 00:07:40,440 parenthesis to the end like so and 203 00:07:40,440 --> 00:07:42,480 notice that the red - the has 204 00:07:42,480 --> 00:07:44,970 disappeared as well as the red lines in 205 00:07:44,970 --> 00:07:47,040 the right side of the screen the right 206 00:07:47,040 --> 00:07:49,230 margin, so now that we haven't got any 207 00:07:49,230 --> 00:07:51,030 errors we can run this again and now 208 00:07:51,030 --> 00:07:52,680 that I've already run the program for 209 00:07:52,680 --> 00:07:54,720 the first time by right clicking I can 210 00:07:54,720 --> 00:07:56,070 just actually come up here to the green 211 00:07:56,070 --> 00:07:58,260 triangle up in the toolbar I can click 212 00:07:58,260 --> 00:08:00,270 on that now that doesn't always work 213 00:08:00,270 --> 00:08:02,250 until you be the right clicked and 214 00:08:02,250 --> 00:08:04,350 chosen the function you want to run or 215 00:08:04,350 --> 00:08:06,900 use the run menu now you can see on 216 00:08:06,900 --> 00:08:08,220 there there's already been different 217 00:08:08,220 --> 00:08:09,540 options for running so I'm click on that 218 00:08:09,540 --> 00:08:11,910 you've got different options there but 219 00:08:11,910 --> 00:08:13,860 we've only got the one file here they've 220 00:08:13,860 --> 00:08:16,500 only got may end up Katie so if you end 221 00:08:16,500 --> 00:08:18,120 up running the wrong program later in 222 00:08:18,120 --> 00:08:20,250 the course either right-click on the 223 00:08:20,250 --> 00:08:22,710 code well it's in the editor that you 224 00:08:22,710 --> 00:08:24,900 want to run or use the menus to come up 225 00:08:24,900 --> 00:08:26,400 here and then choose the actual menu but 226 00:08:26,400 --> 00:08:28,680 in our case we've just got the one menu 227 00:08:28,680 --> 00:08:30,690 of the one file because that's all we've 228 00:08:30,690 --> 00:08:32,220 worked on up until this point in the 229 00:08:32,220 --> 00:08:34,530 course so after that the green button 230 00:08:34,530 --> 00:08:36,090 should work for whatever map function 231 00:08:36,090 --> 00:08:38,220 you're trying to run but you will have 232 00:08:38,220 --> 00:08:40,080 to tell it to run it the first time, 233 00:08:40,080 --> 00:08:44,640 so if I just click on that now you can see 234 00:08:44,640 --> 00:08:45,990 we've got hello world there my first 235 00:08:45,990 --> 00:08:48,120 Kotlin program. So then you've got a 236 00:08:48,120 --> 00:08:50,010 function containing two lines to execute 237 00:08:50,010 --> 00:08:52,080 and unless we do something to tell it 238 00:08:52,080 --> 00:08:54,030 otherwise, Kotlin will just start at the 239 00:08:54,030 --> 00:08:55,620 top and obey the instructions on each 240 00:08:55,620 --> 00:08:58,360 line until it gets to the end, 241 00:08:58,360 --> 00:09:00,340 we can't have a look at what we can do 242 00:09:00,340 --> 00:09:02,620 to make it behave differently shortly 243 00:09:02,620 --> 00:09:04,300 but before that we need to have a look 244 00:09:04,300 --> 00:09:06,730 at variables or properties we'll do that 245 00:09:06,730 --> 00:09:09,450 in the next video.