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

Answer:

conversion No loss of info.
Automatic Conversion.
Possible loss of precision.
Automatic Conversion.
Possible great loss of information.
Requires a Type Cast.
byte to short X   
short to byte   X
short to long X   
int to float  X  
float to byte   X
double to float   X

Example Method Calls

Point  A = new Point();

short  a = 93,    b = 92;
int    i = 12,    j = 13;
long   x =  0,    y = 44;
double u = 13.45, v = 99.2;

A.move( i, j );             // OK --- no conversion needed.
A.move( a, b );             // OK --- shorts converted to ints with no loss
A.move( a, j );             // OK --- short converted to int with no loss

A.move( x, y );             // NOT OK --- possible loss of precision and magnitude.
A.move( (int)x, (int)y );   // OK --- type casts say to go ahead

A.move( i, v );             // NOT OK -- possible loss of precision and magnitude (for v).
A.move( i, (int)v );        // OK --- type cast says to go ahead.

Where there is no loss of information, or only a possible loss of precision, Java automatically converts the value in a method call to what is required. When there is a possible loss of both precision and magnitude, Java requires you to explicitly state that you want to go ahead with the conversion by using a type cast.

It is only the type of the variable (or of the expression) that matters, NOT the value that is in it.

For example, in the first method call marked NOT OK, the particular values could (in this case) be converted from long to int without loss of information. But for many values that could be in a long information would be lost.


QUESTION 9:

Which of the following method calls are correct?

Point  A = new Point();

short  a = 93,    b = 92;
int    i = 12,    j = 13;
long   x =  0,    y = 44;
double u = 13.45, v = 99.2;

A.move( i, b );   //  
A.move( a, x );   //  
A.move( u, b );   //