In this video we're going to go over implicit conversion.
And there are multiple different types of conversion.
We're going to look at them step by step.
So the first one is going to be implicit conversion.
Therefore I create a variable that I'm going to call my int.
And I'm going to assign a value to it let's say 13.
And as always I end this statement with a semicolon.
Now I can have a double which I'm going to call my double.
And I can now assign my int to that double.
So that works.
You see there are no errors no problem whatsoever.
So here I have an implicit conversion from an int to a double.
So this my double is of type double.
And this does work because the int has less or holds less information than the double.
And the double so to speak, can hold all the information that the int can hold.
So there is no loss of data, no loss of information.
When I'm using implicit conversion.
So the target type is guaranteed to be able to hold the information in this case the number 13.
So this is what implicit conversion looks like.
You can use implicit conversion with more complex examples as well.
So you could even have something like a long so long.
My long can also hold my int because a long is just a bigger int, so to speak, because int holds a
32 bit signed integer, which just is a very long number, so to speak, and long can hold an even longer
number.
So it represents a 64 bit signed integer.
So there is no data loss.
However the other way around won't work.
So I cannot say my int is equal to my long.
That won't work because my long can hold more information than my INT, and therefore there would be
data loss and therefore I would have to use a different way to do the conversion.
I cannot use implicit conversion for that.
You can try this out very easily by making a long number.
Okay.
A very long number like this here for example, let's see where it stops.
So this is the length that my int can hold.
And if I check what a double can hold.
So we can just see here can a double hold this long of a number.
Yes it can.
So I can assign my into it.
Can my long hold this long of a number.
Yes it can.
So I can assign my int to it the other way around.
I can't do it because if we look at my long I could assign a even longer number like so and not as long,
but longer.
So this is the length that I can assign here, I think even like this.
So I can assign such a long number into my long, but I cannot assign such a long number into my int.
Okay.
So this is just to show you how you can quickly check if that works.
Now this works also for floats, so you can store a float inside of a double, but you cannot store
a double inside of a float.
So we can check that out by creating my float.
So a float will be my float.
And I'm going to assign my double in here.
And you will see that that won't work.
However, if I have a float and let's assign any type of value in here and the F at the end, and now
I try to store that inside of my double, I will get no problems at all, because I can implicitly convert
from a float into a double, because a double just has more range and therefore has no problem handling
the float.
Okay, so that's implicit conversion.
In the next video we're going to look at explicit conversion.