Type Coercion vs Type Conversion
The key difference between type coercion and type conversion is that type coercion is always implicit, whereas type conversion can be either implicit or explicit.
In other words, “type conversion” refers to the general process of changing a type, whereas type coercion refers more specifically to the implicit conversion of a type. We can further sub-divide type coercion into two kinds – programmer controlled, and compiler controlled (sometimes referred to as juggling). Some languages provide a mechanism to explicitly state the conversion from one specific type and another, for example in C# you can define a conversion using
'3' + 6 is 36. It’s only a problem when the compiler’s behaviour doesn’t match the programmer’s expectation (i.e. the ball not landing in the hand of a juggler – a mixture of surprise and embarrassment).
Here are some pseudocode examples of these operations…
Number num = 1; String str = '2'; // Explicit type conversion Number total = num + str.ToNumber(); // 3 // Implicit type coercion Number total = num + str; // 12 // Type coercion as juggling Number total = num + str; // 2 (1 + str.Length)