Expressions and Assignment Statements
3rd chuck
Outline:
- Next, mixed-mode expressions are described and evaluated. This leads to the definition and evaluation of widening and narrowing type conversions, both implicit and explicit.
Type conversions are either narrowing or widening.
Narrowing: converts a value to a type that cannot store even approximations of all of the values of the original type
- Example: converting a double to a float in Java is a narrowing conversion, because the range of double is much larger than that of float
- Properties: not always safe
Widening: converts a value to a type that can include at least approximations of all of the values of the original type
- Example: converting an int to a float in Java is a widening conversion
- Properties: nearly always safe, however, there is a possibility to result in reduced accuracy
- Example: in many cases, integers are stored in 32 bits, which allows at least 9 decimal digits of precision. But floating-point values are also stored in 32 bits, with only about seven decimal digits of precision (because of the space used for the exponent). So, integer-to-floating-point widening can result in the loss of two digits of precision.
Coercion of non-primitive types are, of course, more complex.
Type conversions can be either explicit or implicit
Languages that allow expressions which has operands of different types, which are called mixed-mode expressions, must define conventions for implicit operand type conversions because computers do not have binary operations that take operands of different types
- Because error detection is reduced when mixed-mode expressions are allowed, F#, Ada, and ML do not allow them
Implicit conversions, or coercion
- Initiated by the compiler or runtime system
- Language designers are not in agreement on the issue of coercion in arithmetic expressions
- Those, against a broad range of coercion, are concerned with the reliability problems that can result from such coercion, because they reduce the benefits of type checking.
- Example:
int a;
float b, c, d;
. . .
d = b * a;
- Explanation: Assume that the second operand of the multiplication operator was supposed to be c, but because of a keying error it was typed as a. Because mixed-mode expressions are legal in Java, the compiler would not detect this as an error. It would simply insert code to coerce the value of the int operand, a, to float. If mixed-mode expressions were not legal in Java, this keying error would have been detected by the compiler as a type error.
- Those who would rather include a wide range of coercion are more concerned with the loss in flexibility that results from restrictions.
Explicit conversions, or casts, not coercion
- explicitly requested by the programmer
- In the C-based languages, explicit type conversions are called casts:
(int)angle
- In ML and F#, the casts have the syntax of function calls:
float(sum)
REFERENCES:
- Sebesta, R. W. (n.d.). Expressions and Assignment Statements. In Concepts of Programming Languages (12th ed., pp. 313–315).