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

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.

In the following situations, the compiler will automatically convert from the type of the expression in a method call to the required type:

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.

"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 the float 123456700.0 shows a loss of precision (but is done, anyway).

QUESTION 7:

Will there be a loss of precision in converting the int 123456789 to a double?