1 00:00:04,980 --> 00:00:07,080 In the previous video we looked at how 2 00:00:07,080 --> 00:00:09,090 to perform arithmetic on numeric 3 00:00:09,090 --> 00:00:12,059 variables. So we saw the basic operations 4 00:00:12,059 --> 00:00:14,309 of addition subtraction multiplication 5 00:00:14,309 --> 00:00:17,520 and division we also discussed the int 6 00:00:17,520 --> 00:00:19,800 and double types and why we need to use 7 00:00:19,800 --> 00:00:21,599 doubles when working with fractional 8 00:00:21,599 --> 00:00:23,610 numbers in this video let's look at 9 00:00:23,610 --> 00:00:26,460 another thing we can do with strings. 10 00:00:26,460 --> 00:00:27,779 Now we saw that tow we cannot perform 11 00:00:27,779 --> 00:00:29,189 arithmetic on them 12 00:00:29,189 --> 00:00:31,770 we can't multiply them for example but 13 00:00:31,770 --> 00:00:33,180 we can do something that looks like 14 00:00:33,180 --> 00:00:36,390 addition, so open up the Kotlin tutorial 15 00:00:36,390 --> 00:00:38,070 project if you haven't already we're 16 00:00:38,070 --> 00:00:40,140 going to start making some changes so 17 00:00:40,140 --> 00:00:41,760 what I'm going to do is modify all our 18 00:00:41,760 --> 00:00:43,890 print line statements in a moment so I'm 19 00:00:43,890 --> 00:00:45,390 going to keep our existing code 20 00:00:45,390 --> 00:00:47,280 and I'm going to add this to the end 21 00:00:47,280 --> 00:00:50,100 let's come down here and basically after 22 00:00:50,100 --> 00:00:52,829 the last our line 26 and we worked on 23 00:00:52,829 --> 00:00:55,320 with the printing and years I'm going to 24 00:00:55,320 --> 00:00:57,239 come down here and type in print line on 25 00:00:57,239 --> 00:01:03,420 line 28, parentheses my name in double 26 00:01:03,420 --> 00:01:10,920 quotes is plus Tim. Now when dealing with 27 00:01:10,920 --> 00:01:13,469 strings the plus symbol doesn't try to 28 00:01:13,469 --> 00:01:15,439 add the strings in a mathematical sense 29 00:01:15,439 --> 00:01:18,119 instead it joins them together and 30 00:01:18,119 --> 00:01:20,399 that's called concatenating the strings 31 00:01:20,399 --> 00:01:23,639 which is just a posh word for joining so 32 00:01:23,639 --> 00:01:25,200 when we run the program that's actually 33 00:01:25,200 --> 00:01:29,520 yeah give it a go you can see down here 34 00:01:29,520 --> 00:01:32,130 we get the message my name is Tim 35 00:01:32,130 --> 00:01:34,619 Buchalka. Now another thing you can 36 00:01:34,619 --> 00:01:37,350 do is add numeric values to a string and 37 00:01:37,350 --> 00:01:40,679 that may seem odd but it's so common to 38 00:01:40,679 --> 00:01:42,149 print numbers out with some text 39 00:01:42,149 --> 00:01:44,459 describing what they are that Kotlin 40 00:01:44,459 --> 00:01:46,439 allows you to use a number, it then 41 00:01:46,439 --> 00:01:48,569 concatenates the number onto the end of 42 00:01:48,569 --> 00:01:50,819 the string then that lets us make the 43 00:01:50,819 --> 00:01:52,859 output a bit more meaningful by adding 44 00:01:52,859 --> 00:01:54,990 some text to the output so I'm going to 45 00:01:54,990 --> 00:01:57,270 modify the code now that prints out the 46 00:01:57,270 --> 00:01:59,609 answer of how many years 130 weeks is, 47 00:01:59,609 --> 00:02:02,909 this is the code on line 26 so we can 48 00:02:02,909 --> 00:02:04,859 actually change that and put double 49 00:02:04,859 --> 00:02:12,540 quotes hundred 30 weeks in years is colon 50 00:02:12,540 --> 00:02:16,130 another type of quote then plus years. 51 00:02:16,130 --> 00:02:18,380 So this makes it more obvious what that 52 00:02:18,380 --> 00:02:21,980 value 2.5 means. Now we've hard-coded the 53 00:02:21,980 --> 00:02:23,720 value 130 in the string 54 00:02:23,720 --> 00:02:25,520 which means we'd have to make a change 55 00:02:25,520 --> 00:02:27,980 in both places if we had a different 56 00:02:27,980 --> 00:02:30,110 number of weeks for example if we want 57 00:02:30,110 --> 00:02:32,240 to calculate how many weeks children 34 58 00:02:32,240 --> 00:02:37,220 weeks is, we could change this to 234 but 59 00:02:37,220 --> 00:02:41,660 when we run it this gives the correct 60 00:02:41,660 --> 00:02:46,700 answer 4.5 but the text isn't right. 61 00:02:46,700 --> 00:02:48,170 Now you might be tempted to add the number 62 00:02:48,170 --> 00:02:49,910 of weeks at the beginning but that won't 63 00:02:49,910 --> 00:02:51,710 work so in other words I can't come 64 00:02:51,710 --> 00:02:59,060 along here and put weeks plus something 65 00:02:59,060 --> 00:03:02,180 like that weeks plus and weeks being the 66 00:03:02,180 --> 00:03:04,760 value plus then the string and then the 67 00:03:04,760 --> 00:03:06,980 years but that doesn't work as you can 68 00:03:06,980 --> 00:03:08,510 see and that's because Kotlin doesn't 69 00:03:08,510 --> 00:03:10,550 allow us to concatenate on two numbers 70 00:03:10,550 --> 00:03:13,250 so because the first variable in the 71 00:03:13,250 --> 00:03:15,140 expression is a number it tries to 72 00:03:15,140 --> 00:03:17,570 perform an addition adding a string 73 00:03:17,570 --> 00:03:19,220 value to a number it doesn't work and we 74 00:03:19,220 --> 00:03:22,490 get the error and the error is none of 75 00:03:22,490 --> 00:03:23,780 the following functions can be called 76 00:03:23,780 --> 00:03:25,940 with the arguments supplied but that's 77 00:03:25,940 --> 00:03:27,440 not the end of the world because there's 78 00:03:27,440 --> 00:03:30,110 a much better way, in fact I suggest you 79 00:03:30,110 --> 00:03:31,910 don't concatenate strings like we've 80 00:03:31,910 --> 00:03:34,010 just done, so probably asking now why 81 00:03:34,010 --> 00:03:35,180 have I shown you how to do something 82 00:03:35,180 --> 00:03:37,580 that I don't recommend you do well this 83 00:03:37,580 --> 00:03:40,010 is the Kotlin for Android course but 84 00:03:40,010 --> 00:03:41,600 when dealing with Android you're going 85 00:03:41,600 --> 00:03:44,060 to see a lot of Java code. Now Java 86 00:03:44,060 --> 00:03:45,710 doesn't have the feature that I'm about 87 00:03:45,710 --> 00:03:47,600 to show you which means you'll see 88 00:03:47,600 --> 00:03:49,370 strings concatenated with the plus 89 00:03:49,370 --> 00:03:50,720 operator in Java. 90 00:03:50,720 --> 00:03:53,210 Now it's useful to be able to read code 91 00:03:53,210 --> 00:03:55,310 like that and understand what it's doing 92 00:03:55,310 --> 00:03:57,200 but there's no reason for you to write 93 00:03:57,200 --> 00:03:59,690 it that way in Kotlin, so I'm going to 94 00:03:59,690 --> 00:04:01,460 undo that last change so the program 95 00:04:01,460 --> 00:04:07,700 runs, so we're back to how it was and it 96 00:04:07,700 --> 00:04:09,920 was working before then I'm going to use 97 00:04:09,920 --> 00:04:11,810 something called string interpolation so 98 00:04:11,810 --> 00:04:13,940 I'm gonna put a dollar sign what you're 99 00:04:13,940 --> 00:04:17,779 going to do is delete the second double 100 00:04:17,779 --> 00:04:20,298 quote and put a dollar sign Tim and then 101 00:04:20,298 --> 00:04:22,400 close off the double quote so you can 102 00:04:22,400 --> 00:04:23,960 see we've got one string but wheezing a 103 00:04:23,960 --> 00:04:26,260 dollar sign Tim within the double quotes 104 00:04:26,260 --> 00:04:30,360 first you know that says make sure it works. 105 00:04:30,360 --> 00:04:32,620 My name is Tim Buchalka so that's 106 00:04:32,620 --> 00:04:35,319 working. Now string interpolation in 107 00:04:35,319 --> 00:04:37,120 Kotlin is just a fancy way of saying 108 00:04:37,120 --> 00:04:40,060 that Kotlin will replace variables with 109 00:04:40,060 --> 00:04:42,639 their values. Now to use a string 110 00:04:42,639 --> 00:04:45,159 interpolation a prefix the variable 111 00:04:45,159 --> 00:04:46,960 name with a dollar symbol like you've 112 00:04:46,960 --> 00:04:50,590 seen me here do on line 28, so dollar Tim 113 00:04:50,590 --> 00:04:52,960 tells Kotlin to use the value of the 114 00:04:52,960 --> 00:04:55,750 variable Tim rather than the 3 character 115 00:04:55,750 --> 00:04:58,090 variable name. Now we can actually use 116 00:04:58,090 --> 00:05:01,449 that on line 26 to include the number of 117 00:05:01,449 --> 00:05:02,800 weeks at the start of the string 118 00:05:02,800 --> 00:05:04,629 some of you use interpolation for the 119 00:05:04,629 --> 00:05:06,789 year as well so we can do that by coming 120 00:05:06,789 --> 00:05:09,159 over here instead of 130 I'm going to 121 00:05:09,159 --> 00:05:15,280 change it to dollar weeks like so and it 122 00:05:15,280 --> 00:05:16,930 also makes it easy to produce more 123 00:05:16,930 --> 00:05:19,090 readable output without using loads of 124 00:05:19,090 --> 00:05:20,770 pluses so let's just fix that string up 125 00:05:20,770 --> 00:05:22,990 in time so we've got dollar weeks weeks 126 00:05:22,990 --> 00:05:25,810 is go ahead in all the time and it 127 00:05:25,810 --> 00:05:29,800 should be so make that is then we're 128 00:05:29,800 --> 00:05:33,400 going to put dollar years I'm gonna 129 00:05:33,400 --> 00:05:35,770 delete the rest of it there and put a 130 00:05:35,770 --> 00:05:37,870 double quote on the end so I've got 131 00:05:37,870 --> 00:05:40,240 dollar weeks space week space is space 132 00:05:40,240 --> 00:05:43,120 dollar years space years all in double 133 00:05:43,120 --> 00:05:45,219 quotes as you can see and that reads 134 00:05:45,219 --> 00:05:46,990 much better if we actually run this to 135 00:05:46,990 --> 00:05:51,759 confirm that it works 234 weeks 136 00:05:51,759 --> 00:05:54,819 is 4.5 years and my name is Tim Burchett 137 00:05:54,819 --> 00:05:57,370 on the next line so whenever you need to 138 00:05:57,370 --> 00:05:59,289 include the value of a variable in a 139 00:05:59,289 --> 00:06:01,000 string you can use string interpolation 140 00:06:01,000 --> 00:06:03,849 by prefixing the variable name with a 141 00:06:03,849 --> 00:06:06,190 dollar symbol. Now if you need to include 142 00:06:06,190 --> 00:06:08,349 more complicated expressions you 143 00:06:08,349 --> 00:06:09,909 surround the expression with curly 144 00:06:09,909 --> 00:06:12,009 braces now you can see that working on 145 00:06:12,009 --> 00:06:15,129 that aligned 21 where we print out a 146 00:06:15,129 --> 00:06:17,710 quarter of our apples so coming down 147 00:06:17,710 --> 00:06:18,719 here - line 21 148 00:06:18,719 --> 00:06:21,099 and I'm going to put double quotes there 149 00:06:21,099 --> 00:06:29,259 a quarter of the apples is then a dollar 150 00:06:29,259 --> 00:06:32,719 sign then a left curly brace 151 00:06:32,719 --> 00:06:34,909 and then a right curly-brace after the 152 00:06:34,909 --> 00:06:37,459 end of the expression then another 153 00:06:37,459 --> 00:06:38,179 double quote 154 00:06:38,179 --> 00:06:40,279 so it basically enclosed the entire 155 00:06:40,279 --> 00:06:42,829 expression in curly braces after the 156 00:06:42,829 --> 00:06:44,929 dollar symbol as you can see there on line 21 157 00:06:44,929 --> 00:06:50,329 now if you run that, a quarter of the apples is 158 00:06:50,329 --> 00:06:51,649 one which is correct 159 00:06:51,649 --> 00:06:53,899 Apple was divided by four apples having 160 00:06:53,899 --> 00:06:57,289 the value of six and obviously we're 161 00:06:57,289 --> 00:06:59,029 getting the number rounded up because 162 00:06:59,029 --> 00:07:02,509 we're actually using integer division so 163 00:07:02,509 --> 00:07:03,999 there's no fractional component output. 164 00:07:03,999 --> 00:07:06,199 Alright so there's still three values 165 00:07:06,199 --> 00:07:08,239 we have no description so it's time for 166 00:07:08,239 --> 00:07:10,279 a challenge. Now throughout the course 167 00:07:10,279 --> 00:07:11,479 there's going to be challenges for you 168 00:07:11,479 --> 00:07:13,669 to try these are designed for you to 169 00:07:13,669 --> 00:07:15,129 check your understanding of the material 170 00:07:15,129 --> 00:07:17,479 we've covered and they'll get 171 00:07:17,479 --> 00:07:19,339 progressively more involved as we work 172 00:07:19,339 --> 00:07:21,079 through the course now they're not a 173 00:07:21,079 --> 00:07:22,879 test they're there for you to check that 174 00:07:22,879 --> 00:07:24,709 you've understood everything it's also 175 00:07:24,709 --> 00:07:26,569 not important that you get exactly the 176 00:07:26,569 --> 00:07:28,909 same solution as me the real test here 177 00:07:28,909 --> 00:07:31,579 is does your solution work if your 178 00:07:31,579 --> 00:07:33,169 solution to a challenge produce the 179 00:07:33,169 --> 00:07:35,299 correct result then you've completed the 180 00:07:35,299 --> 00:07:37,459 challenge successfully alright with that 181 00:07:37,459 --> 00:07:40,680 said let's have a look at our first challenge 182 00:07:40,680 --> 00:07:42,960 so for each of the remaining three 183 00:07:42,960 --> 00:07:44,639 valleys that are being printed without a 184 00:07:44,639 --> 00:07:47,430 description you string interpolation to 185 00:07:47,430 --> 00:07:49,080 display the valleys with some 186 00:07:49,080 --> 00:07:50,970 descriptive text. Now use whatever 187 00:07:50,970 --> 00:07:53,310 descriptive text you want the important 188 00:07:53,310 --> 00:07:55,020 thing is that you include the values in 189 00:07:55,020 --> 00:07:57,630 the text so try that out pause the video 190 00:07:57,630 --> 00:07:59,699 now and come back to this video on 191 00:07:59,699 --> 00:08:01,080 playing again when you're ready to see 192 00:08:01,080 --> 00:08:04,639 in more solution pause the video now. 193 00:08:04,639 --> 00:08:06,660 Okay so how did you got? Hopefully you 194 00:08:06,660 --> 00:08:08,340 managed to come up with a solution for that 195 00:08:08,340 --> 00:08:10,770 so let's actually have a go at string 196 00:08:10,770 --> 00:08:13,440 interpolation for those remaining prints 197 00:08:13,440 --> 00:08:15,389 the printouts and the first one is is 198 00:08:15,389 --> 00:08:17,070 change weekly salary so let's change 199 00:08:17,070 --> 00:08:19,580 that so I'm going to put double quotes 200 00:08:19,580 --> 00:08:22,039 I'm gonna put Tim's 201 00:08:22,039 --> 00:08:27,210 weekly salary is in a dollar sign before 202 00:08:27,210 --> 00:08:29,580 the Tim's weekly salary variable and 203 00:08:29,580 --> 00:08:30,930 then another double quote to close the 204 00:08:30,930 --> 00:08:33,929 line off that's the first one let's 205 00:08:33,929 --> 00:08:35,969 change the second line they are long 12 206 00:08:35,969 --> 00:08:41,419 in double quotes monthly monthly that 207 00:08:41,419 --> 00:08:46,410 comes to and a dollar sign for Tim's 208 00:08:46,410 --> 00:08:49,400 monthly salary and in the double quote 209 00:08:49,400 --> 00:08:52,260 then the next one was down here printed 210 00:08:52,260 --> 00:08:55,020 fruit on line 19 this actually changed 211 00:08:55,020 --> 00:08:57,839 that we're going to put double quote 212 00:08:57,839 --> 00:09:05,650 we'll start with the apples 213 00:09:05,650 --> 00:09:10,730 dollar apples apples miners dollar 214 00:09:10,730 --> 00:09:17,140 oranges oranges please 215 00:09:17,140 --> 00:09:24,080 and then dollar fruit pieces and then s 216 00:09:24,080 --> 00:09:26,420 in parentheses in case the value is one 217 00:09:26,420 --> 00:09:33,020 or less of fruit enclosing double quote 218 00:09:33,020 --> 00:09:36,380 so delight apples apples boilers dollar 219 00:09:36,380 --> 00:09:38,750 oranges oranges leaves dollar fruit 220 00:09:38,750 --> 00:09:41,570 piece or pieces in parentheses of fruit 221 00:09:41,570 --> 00:09:43,430 so let's actually run that and make sure 222 00:09:43,430 --> 00:09:47,390 that it works Tims weekly salary is 223 00:09:47,390 --> 00:09:49,160 thirty two monthly that comes to one 224 00:09:49,160 --> 00:09:51,260 twenty eight and six apples take five 225 00:09:51,260 --> 00:09:54,110 oranges leaves one piece pieces of fruit 226 00:09:54,110 --> 00:09:56,870 so that's the challenge completed so 227 00:09:56,870 --> 00:09:58,340 essentially whenever we want to include 228 00:09:58,340 --> 00:10:00,890 a value in the output string we prefix 229 00:10:00,890 --> 00:10:03,800 the variable with the dollar sign. 230 00:10:03,800 --> 00:10:05,450 for the apples miners oranges have 231 00:10:05,450 --> 00:10:07,040 included that values for the apples and 232 00:10:07,040 --> 00:10:09,110 oranges as you can see there, as well as 233 00:10:09,110 --> 00:10:10,790 the number of fruit then it leaves just 234 00:10:10,790 --> 00:10:12,020 to show you that you can use as many 235 00:10:12,020 --> 00:10:14,330 variables as you need in a string when 236 00:10:14,330 --> 00:10:16,490 using string interpolation there the 237 00:10:16,490 --> 00:10:18,200 challenge only asked you to describe 238 00:10:18,200 --> 00:10:20,330 what the value of one represented if you 239 00:10:20,330 --> 00:10:21,590 haven't included all the variables 240 00:10:21,590 --> 00:10:22,550 that's fine as well. 241 00:10:22,550 --> 00:10:24,620 So if your solution worked and included 242 00:10:24,620 --> 00:10:25,960 texts with each of those values 243 00:10:25,960 --> 00:10:28,880 congratulations now one thing you might 244 00:10:28,880 --> 00:10:30,920 be wondering is how to use the dollar 245 00:10:30,920 --> 00:10:33,200 symbol in strings if it's actually used 246 00:10:33,200 --> 00:10:36,260 for interpolation well turns out Kotlin 247 00:10:36,260 --> 00:10:38,480 is quite clever in that respect if what 248 00:10:38,480 --> 00:10:40,310 follows the dollar sign isn't a very 249 00:10:40,310 --> 00:10:42,590 variable name or an expression inside 250 00:10:42,590 --> 00:10:44,600 curly braces, then the dollars just 251 00:10:44,600 --> 00:10:47,720 treated as an ordinary character so when 252 00:10:47,720 --> 00:10:49,250 printing out my weekly salary we come 253 00:10:49,250 --> 00:10:50,930 back here to have a look on line eleven 254 00:10:50,930 --> 00:10:53,810 we can actually change that to include 255 00:10:53,810 --> 00:10:56,440 the dollar sign for example so Tim's 256 00:10:56,440 --> 00:11:00,710 dollar space then weekly if we run that 257 00:11:00,710 --> 00:11:05,870 we get the example the result we'd 258 00:11:05,870 --> 00:11:08,210 expect Tim's dollar sign weekly salary 259 00:11:08,210 --> 00:11:10,250 so there's nothing special about the 260 00:11:10,250 --> 00:11:12,230 first dollar sign symbol and it just 261 00:11:12,230 --> 00:11:14,630 prints out. Now if we want to prefix the 262 00:11:14,630 --> 00:11:16,460 amount with a dollar that will also work 263 00:11:16,460 --> 00:11:19,030 as well so we can come up here to 264 00:11:19,030 --> 00:11:21,610 that line line 11 again and we could put 265 00:11:21,610 --> 00:11:24,010 a dollar sign before the other dollar 266 00:11:24,010 --> 00:11:27,750 sign and run that again 267 00:11:27,750 --> 00:11:30,040 Tim's dollar weekly salary and then 268 00:11:30,040 --> 00:11:31,180 we've got the dollar sign there showing 269 00:11:31,180 --> 00:11:33,520 before the number thirty-two now if for 270 00:11:33,520 --> 00:11:34,930 some reason we wanted to include a 271 00:11:34,930 --> 00:11:36,880 dollar a dollar sign followed by a 272 00:11:36,880 --> 00:11:39,460 variable name then we have to escape the 273 00:11:39,460 --> 00:11:42,100 dollar using a backslash. Now this is a 274 00:11:42,100 --> 00:11:44,370 bit contrived we're going to print the 275 00:11:44,370 --> 00:11:46,900 print dollar Tim on the line after 276 00:11:46,900 --> 00:11:48,280 printing my name just to show you how to 277 00:11:48,280 --> 00:11:50,920 do it so we'll do something like the 278 00:11:50,920 --> 00:11:55,360 print 'ln double quotes I can print and 279 00:11:55,360 --> 00:12:00,760 print backslash dollar sign Tim if we 280 00:12:00,760 --> 00:12:04,990 run that we're actually printing out the 281 00:12:04,990 --> 00:12:07,240 dollar Tim now as opposed to using 282 00:12:07,240 --> 00:12:09,490 string interpolation to print the value 283 00:12:09,490 --> 00:12:11,710 of the Tim variable there's probably not 284 00:12:11,710 --> 00:12:13,000 awful that you want to do that but 285 00:12:13,000 --> 00:12:14,350 that's how to do what if you do need to 286 00:12:14,350 --> 00:12:16,240 do it. Now there are a few other things 287 00:12:16,240 --> 00:12:18,280 we can deal with strings I'm going to be 288 00:12:18,280 --> 00:12:20,410 explaining them as we use them when 289 00:12:20,410 --> 00:12:23,080 writing the code for our Android apps. 290 00:12:23,080 --> 00:12:25,060 So I'm going to stop the video here, in the 291 00:12:25,060 --> 00:12:26,590 next video we're going to take a look at 292 00:12:26,590 --> 00:12:28,090 the warning ticks in the right margin 293 00:12:28,090 --> 00:12:30,460 and also why I've used well in some 294 00:12:30,460 --> 00:12:33,250 places and var in others, so I will see 295 00:12:33,250 --> 00:12:36,000 you in the next video.