Casting

Type Conversion

Type conversion is the process of changing a value from one data type to another. Programs often need to do this because different types serve different purposes and don’t always mix freely — you can multiply two integers, but you can’t multiply a string and an integer.

There are two kinds of type conversion: implicit and explicit.

Implicit Conversion#

An implicit conversion happens automatically, without you writing anything extra. The language does it for you when it knows the conversion is safe — meaning no data will be lost.