1 00:00:00,270 --> 00:00:00,960 Welcome back. 2 00:00:01,230 --> 00:00:07,890 And this video, I would like to talk about typecasting and very quickly look at lists, so I have this 3 00:00:08,280 --> 00:00:09,210 function main here. 4 00:00:09,510 --> 00:00:18,700 And this typecasting file in which I create a new variable called string list and it is a list of strings. 5 00:00:18,720 --> 00:00:21,090 So that is the syntax that you can use. 6 00:00:21,450 --> 00:00:26,970 And when you want to create a list, then you can use a list of, for example. 7 00:00:27,120 --> 00:00:30,960 So I'm creating a list of Dennis, Frank, Michael and Gary. 8 00:00:31,170 --> 00:00:38,580 So this is a list which can only contain strings, but you can also create a list which has a mixed 9 00:00:38,580 --> 00:00:39,090 types. 10 00:00:39,120 --> 00:00:47,430 For example, using the any keyboard here, we say that we accept any type of object in our list. 11 00:00:47,670 --> 00:00:56,100 So here we can use strings, then we can use integers, but also doubles and any other data type that 12 00:00:56,100 --> 00:00:56,790 is available. 13 00:00:57,900 --> 00:00:59,460 So let's look at this for loop here. 14 00:00:59,970 --> 00:01:02,430 We have value and mix type list. 15 00:01:02,610 --> 00:01:06,180 So what we do is we go through this mix type list with this fall. 16 00:01:06,570 --> 00:01:12,460 And for every single value in that mix type list, we want to run this, if else, block. 17 00:01:12,870 --> 00:01:16,290 So we check if the value is of type integer. 18 00:01:16,470 --> 00:01:19,080 So we use this is keyword here, is it? 19 00:01:19,080 --> 00:01:24,360 And if it's an end, then please print integer and then the value. 20 00:01:25,380 --> 00:01:28,830 Otherwise, if it's type double, please. 21 00:01:29,840 --> 00:01:38,300 Right, double than the value with the floor value, so we floor the value to its next lower full number. 22 00:01:39,350 --> 00:01:47,030 And otherwise, if it is a string, then please write the string and also write the length of the string. 23 00:01:47,660 --> 00:01:52,020 So just write this print statement and in any other case. 24 00:01:52,040 --> 00:01:57,170 So in this blog, I just say it's an unknown type, so I have no idea of what type it is. 25 00:01:58,340 --> 00:01:58,700 All right. 26 00:01:59,030 --> 00:02:00,620 So this is a follow up. 27 00:02:00,620 --> 00:02:05,540 Alternatively, we could have done it with this for loop here, where instead of using if and else, 28 00:02:05,540 --> 00:02:10,970 if we use went and it works pretty much the same way, only the syntax is different. 29 00:02:11,270 --> 00:02:20,090 So can see we use when the value and then in brackets we say is integer, then execute this code. 30 00:02:20,270 --> 00:02:23,240 So what we have here is a bunch of lambda expressions. 31 00:02:25,510 --> 00:02:31,990 And you can also see once you hover over this if statement that it wants you to use when instead. 32 00:02:32,350 --> 00:02:38,260 So it's pretty much doing the same thing as we have done here and this alternative way. 33 00:02:38,740 --> 00:02:39,860 So both ways are fine. 34 00:02:39,880 --> 00:02:42,550 But Caitlin prefers this one approach. 35 00:02:43,450 --> 00:02:44,890 Now just look at smart casts. 36 00:02:45,690 --> 00:02:47,650 A I smartcast is what you can see here. 37 00:02:47,770 --> 00:02:51,430 So I'm creating this value, which I call object one. 38 00:02:51,970 --> 00:02:56,260 It's a variable which is of type any, and it says I have a dream. 39 00:02:56,950 --> 00:02:57,250 All right. 40 00:02:57,250 --> 00:03:05,440 So I store a string in there, but no check is object one a string, and I'm using the exclamation mark 41 00:03:05,440 --> 00:03:05,650 here. 42 00:03:07,550 --> 00:03:12,920 So this means is it not a string, so is object one and not a string? 43 00:03:13,070 --> 00:03:15,320 Then I'm going to say print another string. 44 00:03:15,680 --> 00:03:17,930 Otherwise, I know that it's a string. 45 00:03:18,350 --> 00:03:21,760 So object is automatically cast to a string in the scope. 46 00:03:21,770 --> 00:03:25,040 So I'm saying found a string of length object at length. 47 00:03:25,760 --> 00:03:32,810 So it already knows OK because it is a string we can use it string functionality, such as getting the 48 00:03:32,810 --> 00:03:35,630 length of their variable. 49 00:03:37,460 --> 00:03:42,710 Then there is something called explicit unsafe casting using the key word. 50 00:03:43,880 --> 00:03:45,080 This can go wrong, by the way. 51 00:03:45,260 --> 00:03:51,470 So we have this other variable called SDR one, which is of type string and what it tries to do is it 52 00:03:51,470 --> 00:03:54,410 tries to use object one as a string. 53 00:03:54,950 --> 00:03:59,260 So this will only work if object one is in fact a string. 54 00:03:59,720 --> 00:04:01,460 If not, then this will go wrong. 55 00:04:02,120 --> 00:04:05,270 So what we're doing is we're using it as a string, we're storing it. 56 00:04:05,480 --> 00:04:09,140 It's an SDR one, and then we print the length. 57 00:04:11,270 --> 00:04:16,790 Then we have this other object here, which is of type any again, and we're storing this time an integer 58 00:04:16,790 --> 00:04:17,120 in there. 59 00:04:17,630 --> 00:04:23,000 And what we're trying is about string string object, a string. 60 00:04:23,000 --> 00:04:26,720 So what we're trying to do is to make a string out of this object to. 61 00:04:27,290 --> 00:04:29,090 And then we're printing it. 62 00:04:30,750 --> 00:04:33,450 As I said, this is unsafe casting, this can go wrong. 63 00:04:35,120 --> 00:04:42,500 And then we can also use explicit essays casting using the ask question my keyword, so here object 64 00:04:42,500 --> 00:04:42,890 three. 65 00:04:43,280 --> 00:04:52,820 Any again, the same value and this case we say, OK, it's a string Nullarbor and Object three as high 66 00:04:52,820 --> 00:04:53,180 strength. 67 00:04:53,180 --> 00:04:55,100 So does it work or does it not work? 68 00:04:55,100 --> 00:04:56,780 Well, in this case, it will work. 69 00:04:57,380 --> 00:04:59,800 So if we use that, it will print. 70 00:04:59,810 --> 00:05:01,580 Now what will happen here? 71 00:05:01,970 --> 00:05:03,260 Well, let's find out. 72 00:05:04,460 --> 00:05:05,620 We can just run the code. 73 00:05:07,100 --> 00:05:15,140 And see, so I'm getting an error here, exception and main thread typecast, Katie 47's on this line 74 00:05:15,140 --> 00:05:18,110 here where I'm saying object to a string. 75 00:05:18,380 --> 00:05:19,580 Why does it go wrong? 76 00:05:19,970 --> 00:05:23,900 Well, because object to is of type any and what is behind it. 77 00:05:24,560 --> 00:05:25,430 It's not a string. 78 00:05:25,700 --> 00:05:26,540 It's an integer. 79 00:05:26,900 --> 00:05:28,490 That's why this call doesn't work. 80 00:05:28,940 --> 00:05:32,510 If I, for example, would go ahead and add the question mark here. 81 00:05:32,540 --> 00:05:37,790 Well, now it's not working because the string is not another string. 82 00:05:38,030 --> 00:05:41,690 So I would have to make this also another string and suddenly it would work. 83 00:05:41,960 --> 00:05:44,870 But that's exactly what we did in this case down here. 84 00:05:45,560 --> 00:05:45,980 All right. 85 00:05:47,120 --> 00:05:52,190 So let's get rid of this line here, because it doesn't add any value. 86 00:05:52,940 --> 00:05:55,490 And let's run it again, and there we are. 87 00:05:55,610 --> 00:05:57,050 So now things work. 88 00:05:57,170 --> 00:05:58,220 Let's start from the top. 89 00:05:59,540 --> 00:06:03,110 We say here at the very top. 90 00:06:05,090 --> 00:06:06,620 Starring Dennis. 91 00:06:08,120 --> 00:06:08,900 Of length. 92 00:06:09,050 --> 00:06:09,530 Five. 93 00:06:09,830 --> 00:06:14,090 OK, so we had this mixed type list, so we go through every single element. 94 00:06:14,540 --> 00:06:17,480 Then the next element is an integer 31. 95 00:06:17,990 --> 00:06:26,690 Then it's an integer five, a string b day with the length of four, then a double seventy point five 96 00:06:26,690 --> 00:06:28,220 with the floor value of seventy. 97 00:06:29,670 --> 00:06:34,890 And then string ways of laying six string kg of length to. 98 00:06:36,030 --> 00:06:39,930 And then again, the same with this second for loop, so. 99 00:06:40,350 --> 00:06:41,400 Exactly the same stuff. 100 00:06:42,090 --> 00:06:46,470 And then we are at this point here where we smartcast. 101 00:06:47,520 --> 00:06:47,940 So. 102 00:06:48,860 --> 00:06:51,400 It sets forth a string of length 14. 103 00:06:52,220 --> 00:06:58,440 And that is this else plot, because we are saying here that I have a dream is not, not a string. 104 00:06:58,520 --> 00:07:02,720 So we go into this book and we print this statement out here. 105 00:07:04,140 --> 00:07:11,640 Then we have this statement where it says object as a string and its length is 14, so object one. 106 00:07:11,700 --> 00:07:14,040 What its object one well is, I have a dream. 107 00:07:14,490 --> 00:07:16,560 So of course, its length will be 14. 108 00:07:17,370 --> 00:07:18,940 And then finally, we have null. 109 00:07:19,260 --> 00:07:21,060 And where does this now come from? 110 00:07:21,090 --> 00:07:27,420 Well, it comes from here where we are trying to save casts and we use Nullarbor. 111 00:07:27,600 --> 00:07:31,290 So our R3 is a Nullarbor, so it can be now. 112 00:07:31,830 --> 00:07:36,030 And we're trying to cast when we are not sure whether it's going to work or not. 113 00:07:36,570 --> 00:07:42,300 And in this case, we were wise to do so because otherwise we would have created an error as we have 114 00:07:42,300 --> 00:07:42,630 done. 115 00:07:43,050 --> 00:07:45,060 And the example with Object two? 116 00:07:45,510 --> 00:07:45,900 All right. 117 00:07:45,900 --> 00:07:48,130 So this is a little introduction to typecasting. 118 00:07:48,150 --> 00:07:49,800 Thanks a lot for your attention.