1
00:00:00,180 --> 00:00:00,810
Welcome back.

2
00:00:01,140 --> 00:00:08,100
Now we're going to look at unsafe and safe cast operators, and they are useful because sometimes you

3
00:00:08,100 --> 00:00:10,620
just need to cast something right?

4
00:00:10,780 --> 00:00:14,750
So you have something in a specific type and you need to cast it in another type.

5
00:00:15,000 --> 00:00:17,550
And that's where the aski word comes in.

6
00:00:18,040 --> 00:00:20,650
And this is an unsafe cast operator.

7
00:00:20,700 --> 00:00:24,570
Sometimes it is not possible to cast a variable and it throws an exception.

8
00:00:24,840 --> 00:00:27,120
And this is called an unsafe cast.

9
00:00:27,590 --> 00:00:31,380
The answer, of course, is performed by the infix operator s.

10
00:00:31,890 --> 00:00:34,290
And let's have a look at an example here.

11
00:00:34,470 --> 00:00:41,220
A nullable string with a question mark cannot be cast to a nonvolatile string.

12
00:00:41,370 --> 00:00:45,210
So a string without a question mark because this then throws an exception.

13
00:00:45,240 --> 00:00:47,340
So let's look at this function, Menhir.

14
00:00:47,550 --> 00:00:55,920
We have this value object of a type any nullable, which is now and then we have this value string or

15
00:00:55,930 --> 00:00:57,720
star, which is a type string.

16
00:00:57,870 --> 00:01:06,480
And what we try to do is we try to cast object, so object as string and then print that string that

17
00:01:06,480 --> 00:01:07,210
we created.

18
00:01:07,650 --> 00:01:11,460
So the problem is that we get an error here, an exception to be precise.

19
00:01:11,790 --> 00:01:19,560
It says exception in thread mean kotlin typecast exception now cannot be cast to non null type in Scotland

20
00:01:19,560 --> 00:01:20,000
string.

21
00:01:20,220 --> 00:01:28,590
So here, of course this doesn't work because the object of type any nullable is actually null.

22
00:01:28,830 --> 00:01:29,690
So it's empty.

23
00:01:30,210 --> 00:01:37,170
But this frozen arrow, so this really Kresh, our software, our program, and we want to avoid that

24
00:01:37,170 --> 00:01:37,830
at all cost.

25
00:01:37,830 --> 00:01:38,070
Right.

26
00:01:38,090 --> 00:01:39,930
So what can we do?

27
00:01:40,320 --> 00:01:42,120
Well, let's have a look in a second.

28
00:01:42,120 --> 00:01:48,480
But first, it generates a class cast exception, trying to cast an integer value of the any type into

29
00:01:48,480 --> 00:01:51,160
a string type leads to a class cast exception.

30
00:01:51,480 --> 00:01:58,860
So here we have an object of type any one, two, three, and we try to cast that to a string so we

31
00:01:58,860 --> 00:02:01,530
save all string or star of type.

32
00:02:01,530 --> 00:02:03,870
String should be object as string.

33
00:02:04,590 --> 00:02:12,150
This throws a class cast exception because any could be any typewrite that's just interprets whatever

34
00:02:12,150 --> 00:02:16,860
data it gets and it interprets in this case that one on 23 is an integer.

35
00:02:17,070 --> 00:02:20,340
So it's going to say that integer cannot be cast to a string.

36
00:02:20,860 --> 00:02:22,820
We need to use another approach.

37
00:02:22,940 --> 00:02:23,260
Right.

38
00:02:23,300 --> 00:02:29,240
We cannot cast we would need to use type conversion or parsing here.

39
00:02:29,520 --> 00:02:29,940
All right.

40
00:02:30,810 --> 00:02:33,730
So now let's look at an example where this actually works.

41
00:02:33,750 --> 00:02:38,970
So the source and the target variable needs to be knuckleballs for casting to work.

42
00:02:39,030 --> 00:02:44,750
So here we have the object to any which is of type nullable.

43
00:02:44,760 --> 00:02:47,570
So we have this question mark and has a string in there.

44
00:02:47,610 --> 00:02:51,180
So a string is hidden, so to speak, in the any object.

45
00:02:51,570 --> 00:02:54,980
And then we have a string, which is also a nullable.

46
00:02:55,380 --> 00:02:59,160
So what we can do is we can say object should be.

47
00:03:00,220 --> 00:03:08,230
Cast into a string, so it should be a type string nullable, so this will all put the string on safe

48
00:03:08,230 --> 00:03:08,590
cast.

49
00:03:09,490 --> 00:03:12,560
There is an operator, however, which makes our life even easier.

50
00:03:12,610 --> 00:03:18,150
So there is something called Save Cast Operator and there as question mark.

51
00:03:18,160 --> 00:03:23,790
So the save cast operator provides a save cast operation to safely cast to a type it returns.

52
00:03:23,900 --> 00:03:28,940
Now, if casting is not possible rather than throwing and cast exception.

53
00:03:29,000 --> 00:03:29,660
Exception.

54
00:03:29,900 --> 00:03:34,510
OK, so this is very good because then our program will not crash.

55
00:03:34,510 --> 00:03:37,420
If it doesn't work, it will just return not.

56
00:03:38,530 --> 00:03:38,880
All right.

57
00:03:38,890 --> 00:03:41,010
So here example of save operation.

58
00:03:41,320 --> 00:03:45,850
So we have a location which is of type any as it actually is a string.

59
00:03:45,850 --> 00:03:46,060
Right.

60
00:03:46,070 --> 00:03:49,400
So in the background there's a string and as you can see, it's not nullable any.

61
00:03:49,810 --> 00:03:53,500
Then we have a safe string, which is a string that is available.

62
00:03:53,740 --> 00:03:58,960
And what it tries to do, it tries to get the location as a string.

63
00:03:59,620 --> 00:04:02,560
So it uses the safe cast operation here.

64
00:04:02,890 --> 00:04:08,320
Then we have a safe int, which is an integer nullable and it tries the same thing.

65
00:04:08,320 --> 00:04:11,560
So it tries to get the location as an integer.

66
00:04:11,980 --> 00:04:15,460
But of course, a text cannot be an integer.

67
00:04:15,460 --> 00:04:15,730
Right.

68
00:04:15,730 --> 00:04:18,940
Or at least a worse as we see here.

69
00:04:18,940 --> 00:04:22,300
Kotlin cannot be of type integer, not even integer nullable.

70
00:04:22,630 --> 00:04:23,920
It will just be not right.

71
00:04:24,130 --> 00:04:27,970
So the output here is kotlin and no.

72
00:04:28,330 --> 00:04:28,660
All right.

73
00:04:29,020 --> 00:04:30,190
Four to save string.

74
00:04:30,400 --> 00:04:37,940
And now for the safe int because it wasn't able to convert it because Kotlin is not convertible or Casterbridge

75
00:04:37,990 --> 00:04:38,650
so to speak.

76
00:04:38,920 --> 00:04:42,160
It's not Costabile into an integer.

77
00:04:44,760 --> 00:04:50,790
All right, so that's it for unsafe and safe casting operators for now and yeah, further in the course,

78
00:04:50,790 --> 00:04:56,400
we will use it and then it will make a lot more sense because here the examples, of course, are very

79
00:04:56,400 --> 00:04:58,980
abstract and they are very purpose driven.

80
00:04:58,980 --> 00:05:01,980
But later on, we will see how it actually is going to work.

81
00:05:02,370 --> 00:05:02,810
All right.

82
00:05:03,180 --> 00:05:04,950
So see you in the next video.

