Type conversion is the most general of these concepts, in that the other two concepts (i.e., casting and coercion) are instances of conversion. Conversion refers to either implicitly or explicitly changing a value from one data type to another. For instance, converting an integer into a floating-point number is an example of conversion. The storage requirements (e.g., from 32 bits to 64 bits) of a value may change as a result of conversion. Type conversions can be either implicit or explicit.
Coercion is an implicit conversion in which values can deviate from the type required by an operator or function without warning or error because the appropriate conversions are made automatically before or at run-time and are transparent to the programmer. The following C program demonstrates coercion:
There are five coercions in this program: one each on lines 8, 14, and 21, and two on line 25. Notice also that coercion happens automatically without any intervention from the programmer.
While the details of how coercions happen can be complex and vary from language to language, when integers and floating-point numbers are operands to an arithmetic operator, the integers are usually coerced into floating-point numbers. For example, a coercion is made from an integer to a floating-point number when mixing an integer and a floating-point number with the addition operator; likewise, a coercion is made from a floating-point number to an integer when mixing an integer and a floating-point number with the division operator. In the program just given, when adding an integer and a floating-point number on line 21, the integer (1) is coerced into a floating-point number (1.0) and the result is a floating-point number (line 37).
Such implicit conversions are generally a language implementation issue and dependent on the targeted hardware platform and operating system (because of storage implications). Consequently, language specifications and standards might be general or silent on how coercions happen and leave such decisions to the language implementer. In some cases, the results are predictable:
In this program, a value of a type requiring less storage can be generally coerced (or cast) into one requiring more storage without loss of data (lines 18 and 40). However, a value of a type requiring more storage cannot generally be coerced (or cast) into one requiring less storage without loss of data (lines 27 and 41).
In the program coercion.c, when the floating-point result of adding an integer and a floating-point number is assigned to a variable of type int (line 25), unlike the results of the expressions on lines 8 and 14 (lines 34 and 36, respectively), it remains a floating-point number (line 39). Thus, there are no guarantees with coercion. The programmer forfeits a level of control depending on the language implementation, hardware platform, and OS being used. As a result, coercion, while offering flexibility and relieving the programmer of the burden of using explicit conversions when deviating from the types required by an operator or function, is generally unpredictable, rendering a program using coercion less safe. Moreover, while coercions between values of differing types add flexibility to a program and can be convenient from the programmer’s perspective when intended, they also happen automatically—and so can be a source of difficult-to-detect bugs (because of the lack of warnings or errors before run-time) when unintended. Java does not perform coercion, as seen in this program:
Java performs no coercion, even between floats and doubles:
There are two forms of explicit type conversions: type casts and conversion functions. A type cast is an explicit conversion that entails interpreting the bit pattern used to represent a value of a particular type as another type. For instance, integer division in C truncates the fractional part of the result, which means that the result must be cast to a float-pointing number to retain the fractional part:
Here, a type cast, (float), is used on line 11 so that the result of the expression 10/3 is interpreted as a floating-point number (line 17) rather than an integer (line 16).
Some languages also support built-in or library functions to convert values from one data type to another. For example, the following C program invokes the standard C library function strtol, which converts a string representing an integer into the corresponding long integer, to convert the string "250" to the integer 250:3
Since the statically typed language ML does not have coercion, it needs provisions for converting values between types. ML supports conversions of values between types through functions. Conversion functions are necessary in Haskell, even though types can be mixed in some Haskell expressions.
52.15.245.1