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 key 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 unsafe cast is performed by the in fix operator asks.
And that's having it.
Look at an example here.
A Nullarbor string with a question mark here cannot be cast to a non-bailable string.
So a string without the question mark, because this then throws an exception.
So let's look at this function main here.
We have this value object of a type, any Nullarbor, which is now and then we have this value string,
or SDR, which is of type string.
And what we tried to do is we tried 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
main Scotland typecast exception null cannot be cast to non null type in Scotland string.
So here, of course, this doesn't work because the object of type any Nullarbor is actually not.
So it's empty, but this frozen arrow.
So this really crashes our software, our program, and we want to avoid that at all cost rate.
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 object of type any one two three, and we had try to cast the to a string.
So we save all string or SDR of type.
String should be object as string.
This throws a class cast exception because any could be any type right that just interprets whatever
data it gets and it interprets.
In this case that 123 is an integer, so it's going to say that integer cannot be cast to a string.
We need to use another approach here, right?
We cannot cast.
We would need to use type conversion or parsing year.
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 Nullarbor.
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 Nullarbor.
So what we can do is we can say object should be.
Cast into a string, so it should be of type string Nullarbor.
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 safe cast operator and there as question mark.
So to Safe Cast Operator provides a safe cast operation to safely cast to a type.
It returns.
I know if casting is not possible, rather than throwing and class cast exception.
Exception.
Okay, so this is very good because then our program will not crash.
If it doesn't work, it will just return null.
All right, so here.
Example of safe cast 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 Nullarbor any.
Then we have a safe string, which is a string that is a Nullarbor and what it tries to do.
It tries to get the location as a string, so it uses a safe cast operation here.
Then we have a safe end, which is an integer noble 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 Nullarbor.
It will just be null, right?
So the output here is codling and no.
All right, Kotlin for to save string and null for the safe int because it wasn't able to convert it.
Because Kotlin is not convertible or testable, so to speak.
It's not Kasturba into an integer.
All right, so that's it for on safe and safe casting operators for now and yeah, furthering 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.