Welcome back.
Now we're going to look at unsafe and safe cast operators, and they are useful because sometimes you
just need to cast something right?
So you have something in a specific type and you need to cast it in another type.
And that's where the aski word comes in.
And this is an unsafe cast operator.
Sometimes it is not possible to cast a variable and it throws an exception.
And this is called an unsafe cast.
The answer, of course, is performed by the infix operator s.
And let's have a look at an example here.
A nullable string with a question mark cannot be cast to a nonvolatile string.
So a string without a question mark because this then throws an exception.
So let's look at this function, Menhir.
We have this value object of a type any nullable, which is now and then we have this value string or
star, which is a type string.
And what we try to do is we try to cast object, so object as string and then print that string that
we created.
So the problem is that we get an error here, an exception to be precise.
It says exception in thread mean kotlin typecast exception now cannot be cast to non null type in Scotland
string.
So here, of course this doesn't work because the object of type any nullable is actually null.
So it's empty.
But this frozen arrow, so this really Kresh, our software, our program, and we want to avoid that
at all cost.
Right.
So what can we do?
Well, let's have a look in a second.
But first, it generates a class cast exception, trying to cast an integer value of the any type into
a string type leads to a class cast exception.
So here we have an object of type any one, two, three, and we try to cast that to a string so we
save all string or star of type.
String should be object as string.
This throws a class cast exception because any could be any typewrite that's just interprets whatever
data it gets and it interprets in this case that one on 23 is an integer.
So it's going to say that integer cannot be cast to a string.
We need to use another approach.
Right.
We cannot cast we would need to use type conversion or parsing here.
All right.
So now let's look at an example where this actually works.
So the source and the target variable needs to be knuckleballs for casting to work.
So here we have the object to any which is of type nullable.
So we have this question mark and has a string in there.
So a string is hidden, so to speak, in the any object.
And then we have a string, which is also a nullable.
So what we can do is we can say object should be.
Cast into a string, so it should be a type string nullable, so this will all put the string on safe
cast.
There is an operator, however, which makes our life even easier.
So there is something called Save Cast Operator and there as question mark.
So the save cast operator provides a save cast operation to safely cast to a type it returns.
Now, if casting is not possible rather than throwing and cast exception.
Exception.
OK, so this is very good because then our program will not crash.
If it doesn't work, it will just return not.
All right.
So here example of save operation.
So we have a location which is of type any as it actually is a string.
Right.
So in the background there's a string and as you can see, it's not nullable any.
Then we have a safe string, which is a string that is available.
And what it tries to do, it tries to get the location as a string.
So it uses the safe cast operation here.
Then we have a safe int, which is an integer nullable and it tries the same thing.
So it tries to get the location as an integer.
But of course, a text cannot be an integer.
Right.
Or at least a worse as we see here.
Kotlin cannot be of type integer, not even integer nullable.
It will just be not right.
So the output here is kotlin and no.
All right.
Four to save string.
And now for the safe int because it wasn't able to convert it because Kotlin is not convertible or Casterbridge
so to speak.
It's not Costabile into an integer.
All right, so that's it for unsafe and safe casting operators for now and yeah, further in the course,
we will use it and then it will make a lot more sense because here the examples, of course, are very
abstract and they are very purpose driven.
But later on, we will see how it actually is going to work.
All right.
So see you in the next video.