go to previous page   go to home page   go to next page

What will this program write to the monitor?

Answer:

New location:java.awt.Point[x=12,y=42]

Safe Conversions

Automatic Conversions
  • Converting an integer type to another integer type that uses more bits.
  • Converting a floating point type to another floating point type that uses more bits.
  • Converting an integer type to a floating point type that uses the same number of bits may result in a loss of precision, but will be done automatically.
  • Converting an integer type to a floating point type that uses more bits will not result in loss of precision and will be done automatically.

In general, if information might be LOST, a conversion from one type to another will NOT be performed automatically. A conversion from a data type that uses N bits to a type that uses fewer than N bits risks information loss, and will NOT be performed automatically. The compiler makes this decision by examining the data types involved, not the actual values involved.

Note that it is the data types involved and not the actual data currently in a variable that determines if a conversion is safe.

"Loss of precision" means that some of the less significant digits may become zeros, but the most important digits and the size of the number will remain. Recall (from chapter 8) that float has only about seven decimal digits of precision. For example, converting the int 123456789 to a float 123456700.0 shows a loss of precision.


QUESTION 7:

Is there a loss of precision in converting the int 123456789 to a double in the following?

int a = 123456789;
double x = a;