1 00:00:00,120 --> 00:00:00,780 Welcome back. 2 00:00:01,140 --> 00:00:08,070 Now we're going to look at unsafe and safe cast operators, and they are useful because sometimes you 3 00:00:08,070 --> 00:00:10,590 just need to cast something right? 4 00:00:10,800 --> 00:00:14,730 So you have something in a specific type and you need to cast it in another type. 5 00:00:15,030 --> 00:00:17,550 And that's where the key word comes in. 6 00:00:18,060 --> 00:00:20,640 And this is an unsafe cast operator. 7 00:00:20,700 --> 00:00:24,540 Sometimes it is not possible to cast a variable, and it throws an exception. 8 00:00:24,840 --> 00:00:27,060 And this is called an unsafe cast. 9 00:00:27,570 --> 00:00:31,350 The unsafe cast is performed by the in fix operator asks. 10 00:00:31,920 --> 00:00:33,030 And that's having it. 11 00:00:33,030 --> 00:00:34,230 Look at an example here. 12 00:00:34,470 --> 00:00:41,190 A Nullarbor string with a question mark here cannot be cast to a non-bailable string. 13 00:00:41,400 --> 00:00:45,200 So a string without the question mark, because this then throws an exception. 14 00:00:45,210 --> 00:00:47,310 So let's look at this function main here. 15 00:00:47,550 --> 00:00:55,110 We have this value object of a type, any Nullarbor, which is now and then we have this value string, 16 00:00:55,680 --> 00:00:57,690 or SDR, which is of type string. 17 00:00:57,900 --> 00:01:06,300 And what we tried to do is we tried to cast object, so object as string and then print that string 18 00:01:06,300 --> 00:01:07,200 that we created. 19 00:01:07,650 --> 00:01:13,440 So the problem is that we get an error here, an exception to be precise, it says exception in thread 20 00:01:13,440 --> 00:01:19,980 main Scotland typecast exception null cannot be cast to non null type in Scotland string. 21 00:01:20,250 --> 00:01:28,560 So here, of course, this doesn't work because the object of type any Nullarbor is actually not. 22 00:01:28,860 --> 00:01:32,070 So it's empty, but this frozen arrow. 23 00:01:32,070 --> 00:01:38,070 So this really crashes our software, our program, and we want to avoid that at all cost rate. 24 00:01:38,070 --> 00:01:39,930 So what can we do? 25 00:01:40,320 --> 00:01:45,060 Well, let's have a look in a second, but first it generates a class cast exception. 26 00:01:45,060 --> 00:01:51,150 Trying to cast an integer value of the any type into a string type leads to a class cast exception. 27 00:01:51,510 --> 00:01:58,340 So here we have object of type any one two three, and we had try to cast the to a string. 28 00:01:58,350 --> 00:02:01,500 So we save all string or SDR of type. 29 00:02:01,500 --> 00:02:03,870 String should be object as string. 30 00:02:04,590 --> 00:02:12,150 This throws a class cast exception because any could be any type right that just interprets whatever 31 00:02:12,150 --> 00:02:14,070 data it gets and it interprets. 32 00:02:14,070 --> 00:02:20,310 In this case that 123 is an integer, so it's going to say that integer cannot be cast to a string. 33 00:02:20,850 --> 00:02:23,260 We need to use another approach here, right? 34 00:02:23,280 --> 00:02:24,150 We cannot cast. 35 00:02:24,420 --> 00:02:29,250 We would need to use type conversion or parsing year. 36 00:02:29,550 --> 00:02:29,910 All right. 37 00:02:30,810 --> 00:02:33,630 So now let's look at an example where this actually works. 38 00:02:33,750 --> 00:02:38,850 So the source and the target variable needs to be knuckleballs for casting to work. 39 00:02:39,060 --> 00:02:44,730 So here we have the object to any which is of type Nullarbor. 40 00:02:44,760 --> 00:02:47,570 So we have this question mark and has a string in there. 41 00:02:47,580 --> 00:02:51,150 So a string is hidden, so to speak, in the any object. 42 00:02:51,600 --> 00:02:54,960 And then we have a string, which is also a Nullarbor. 43 00:02:55,410 --> 00:02:59,130 So what we can do is we can say object should be. 44 00:03:00,250 --> 00:03:05,170 Cast into a string, so it should be of type string Nullarbor. 45 00:03:05,560 --> 00:03:08,560 So this will all put the string on safe cast. 46 00:03:09,460 --> 00:03:12,520 There is an operator, however, which makes our life even easier. 47 00:03:12,610 --> 00:03:18,130 So there is something called safe cast operator and there as question mark. 48 00:03:18,130 --> 00:03:22,720 So to Safe Cast Operator provides a safe cast operation to safely cast to a type. 49 00:03:23,200 --> 00:03:23,800 It returns. 50 00:03:23,800 --> 00:03:28,840 I know if casting is not possible, rather than throwing and class cast exception. 51 00:03:28,990 --> 00:03:29,630 Exception. 52 00:03:29,950 --> 00:03:34,480 Okay, so this is very good because then our program will not crash. 53 00:03:34,480 --> 00:03:37,350 If it doesn't work, it will just return null. 54 00:03:38,560 --> 00:03:39,370 All right, so here. 55 00:03:39,400 --> 00:03:40,990 Example of safe cast operation. 56 00:03:41,320 --> 00:03:46,030 So we have a location which is of type any as it actually is a string, right? 57 00:03:46,030 --> 00:03:47,500 So in the background, there's a string. 58 00:03:47,740 --> 00:03:49,390 And as you can see, it's not Nullarbor any. 59 00:03:49,810 --> 00:03:55,030 Then we have a safe string, which is a string that is a Nullarbor and what it tries to do. 60 00:03:55,030 --> 00:04:02,530 It tries to get the location as a string, so it uses a safe cast operation here. 61 00:04:02,920 --> 00:04:09,460 Then we have a safe end, which is an integer noble and it tries the same thing, so it tries to get 62 00:04:09,460 --> 00:04:11,530 the location as an integer. 63 00:04:11,980 --> 00:04:17,769 But of course, a text cannot be an integer right or at least a worse. 64 00:04:18,160 --> 00:04:22,250 As we see here, Kotlin cannot be of type integer, not even integer Nullarbor. 65 00:04:22,660 --> 00:04:23,890 It will just be null, right? 66 00:04:24,160 --> 00:04:27,940 So the output here is codling and no. 67 00:04:28,330 --> 00:04:33,700 All right, Kotlin for to save string and null for the safe int because it wasn't able to convert it. 68 00:04:34,240 --> 00:04:38,650 Because Kotlin is not convertible or testable, so to speak. 69 00:04:38,920 --> 00:04:42,130 It's not Kasturba into an integer. 70 00:04:44,790 --> 00:04:50,760 All right, so that's it for on safe and safe casting operators for now and yeah, furthering the course, 71 00:04:50,760 --> 00:04:56,370 we will use it and then it will make a lot more sense because here the examples, of course, are very 72 00:04:56,370 --> 00:04:58,950 abstract and they are very purpose driven. 73 00:04:58,950 --> 00:05:01,950 But later on, we will see how it actually is going to work. 74 00:05:02,400 --> 00:05:04,920 All right, so see you in the next video.