Casting
how to change data type mid way in the code
Casting
Assigning a variable or literal in another type to a variable is possible through casting. Saving a lower-bit value to a higher-bit container is often safe, but the converse is always unsafe because information is lost by losing bits.
(i.e.
putting things from a small box to a big box is safe as we can put everything inside the big box,
but putting things from a big box to a small box is unsafe because the small box might not have enough space to hold everything and the remaining things might be losted.)
Below are some examples:
The type conversion above is an implicit casting, i.e. the compiler itself figures out the converted type. Usually, instead, we would prefer a safer approach, that we specify the converted type in our codes.
Last updated