CSADPRG_mod4_type_conversions
��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������original file
this is unedited
narrowing conversion
- converts a value to a type that cannot store even approximations of all the values of the original type
- ex: double -> float conversion
public static void main(String[] args) {
double x = 2.256891231231;
float y = (float) x;
System.out.printf("x = %.12f",x);
System.out.printf("\ny = %.12f",y);
}
//output x = 2.256891231231 y = 2.256891250610
widening conversion
- generally more safe -> more precision
- converts a value to a type that can include at least approximations of all the values of the original type
mixed-mode expressions
- expressions that can have operands of different types
- Since computers do not have binary operations that can take operands of different types, languages that allow mixed-mode expressions must define conventions for implicit type conversions.
- Coercion is an implicit type conversion initiated by the compiler or the runtime system. Type conversions requested by the programmer are referred to as explicit conversions or casts.
disadvantages of coercions
- reliability concerns
- reduce benefits of type checking
int a;
float b, c, d;
. . .
d = b * a;
- Assume that the second operand was supposed to be c but because of a keying error it was typed as a. Because this is legal in Java, the compiler will not see this as an error and will simply coerce the value of the int to a float. But should errors like this be the concern of the programmer or should the compiler detect them?
explicit type conversions
- c-based languages: explicit type conversions are called CASTS
- may produce warning messages; cases where an explicit narrowing conversion results in a significant change to the value being converted
float x = 2.245;
int y = (int) x;
errors in expressions during evaluation
- If the language requires type checking then type errors cannot occur unless they are caused by coercions of operands.
- Other errors are due the limitations of computer arithmetic (overflow or underflow).
- Consider the sample program below written in C. Suppose that int has a range of -2,147,483,648 to 2,147,483,647.
- errors can also be caused by inherent limitations of arithmetic
int x = 2147483647 + 1;
printf("%d",x);
/* output:
main.c:13:25: warning: integer overflow in expression [-Woverflow] -2147483648
*/
public static void main(String[] args) {
int x = 2/0;
}
/* output
Exception in thread "main" java.lang.ArithmeticException: / by zero at Main.main(Main.java:12)
*/
- run-time errors or exceptions������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������