Welcome back.
And this video, I would like to talk about typecasting and very quickly and look at lists so I have
this function men here and this typecasting file in which I create a new variable called String List,
and it is a list of strings.
So that is the syntax that you can use.
And when you want to create a list, then you can use a list of, for example.
So I'm creating a list of Dennis, Frank, Michael and Gary.
So this is a list which can only contain strings, but you can also create a list which has a mixed
types.
For example, using the any word here, we say that we accept any type of object in our list.
So here we can use strings, then we can use integers, but also doubles and any other data type that
is available.
So let's look at this for loop here.
We have value in mix type list.
So what we do is we go through this mix type list with this volume.
And for every single value in that mix type list, we want to run this if else block.
So we check if the value is of type integer.
So we use this is key word here, is it?
And if it's an end, then please print integer and then the value.
Otherwise if it's of type double please.
Right, double than the value with the floor value, so we floor the value to its next lower full number
and otherwise if it is a string, then please write the string and also write the length of the string.
So just write this print statement and in any other case.
So in this blog, I just say it's an unknown type, so I have no idea of what type it is.
All right.
So this is a follow up.
Alternatively, we could have done it with this follow up here where instead of using if and else if
we use when and it works pretty much the same way, only the syntax is different.
So can see we use when the value and then in brackets we say is integer, then execute this code.
So what we have here is a bunch of lambda expressions.
And you can also see once you hover over this if statement that it wants you to use when instead.
So it's pretty much doing the same thing as we have done here in this alternative way.
So both ways are fine, but Kotlin prefers this one approach.
Now, let's look at smart tests.
A smart cost is what you can see here.
So I'm creating this value, which I called object one.
It's a variable which is of type any and it says I have a dream.
All right.
So I store a string in there.
Then I check is object one a string and I'm using the exclamation mark here.
So this means, is it not a string, so is object one and not a string, then I'm going to say PRINDLE
and not a string.
Otherwise I know that it's a string.
So object is automatically cast to a string in this scope.
So I'm saying found a string of length object at length so it already knows.
OK, because it is a string, we can use it string functionality such as getting the length of the variable.
Then there is something called explicit unsafe casting using the key word.
This can go wrong, by the way, so we have this other variable called Star one, which is of type string,
and what it tries to do is it tries to use object one as a string.
So this will only work if object one is in fact a string.
If not, then this will go wrong.
So what we're doing is we're using it as a string, we're starring in it in Star one, and then we print
the length.
Then we have this other object here, which is of type any again, and we're storing this time an integer
in there, and what we're trying is ball, string, string object, a string.
So what we're trying to do is to make a string out of this object, too, and then we're printing it.
As I said, this is unsafe casting, this can go wrong.
And then we can also use explicit safe casting using the ask question mark keyword.
So here, object three any again to same value.
And this case we say, OK, it's a string nullable and object three as a string.
So does it work or does it not work?
Well, in this case it will work.
So if we use that, it will print.
Now, what will happen here?
Well, let's find out.
We can just run the coat.
And see, so I'm getting an error here, except the main threat typecast Katie 47.
So in this line here where I'm saying object to a string.
Why does it go wrong?
Well, because object two is of type any.
And what is behind it is not a string.
It's an integer.
That's why this call doesn't work.
If I, for example, would go ahead and add a question mark here.
Well, now it's not working because the string is not the nullable string.
So I would have to make this also a nullable string and suddenly it would work.
But that's exactly what we did in this case down here.
All right.
So let's get rid of this line here, because it doesn't add any value and let's try it again and we
are.
So now things work.
Let's start from the top.
We say here at the very top.
String Dennis.
Of length five.
OK, so we had this mixed type list, so we go through every single element, then the next element
is an integer 31, then it's an integer five, a string bidet with the length of a four, then a double
seventeen point five with the floor value of 70.
And then string ways of six string kg of length, too, and then again, the same with this second for
loop, so exactly the same stuff.
And then we are at this point here where we smart kest.
So.
It says found a string of length 14, and that is this ELT's block, because we are saying here that
I have a dream is not not a string.
So we go into this house block and we print this statement out here.
Then we have this statement where it says, Object as a string and its length is 14.
So object one, what is object one?
Well, it's I have a dream.
So, of course, its length will be 14.
And then finally we have now.
And where does this now come from?
Well, it comes from here where we are trying to save casts and we use Nullable.
So our Star three is a nullable.
So it can be now and we're trying to cast when we are not sure whether it's going to work or not.
And in this case, we were wise to do so because otherwise we would have created an error as we have
done.
And the example with object to.
All right.
So this is a little introduction to typecasting.
Thanks a lot for your attention.