"And now for the most interesting part. I'll tell you about type conversions. Variables can't change their types, but there is a place where you can convert types. That place is an assignment operation."

"You can assign variables of different types to each other. In doing so, the value of one variable (of a particular type) is converted into a value of the other type and assigned to the other variable."

"We can point out two types of conversions: widening primitive conversions and narrowing primitive conversions. Widening is like moving things from a small basket to a big one. The procedure is unremarkable and painless. Narrowing is analogous to taking things out of a big basket and putting them in a smaller one. When you do this you may not have enough space; you may have to throw something out."

"Here are the types sorted according their 'basket' size:"

Type conversion

"Just a couple of comments:

1. char's basket is the same size as short's, but you can't move things freely from one to the other. When you move values from short to char, values that are less than 0 will always be lost. When you move values from char to short, values greater than 32,000 will be lost.

2. When you transform integers into fractional numbers, the least significant digits of the number may be thrown out. However, this is acceptable, since the purpose of a fractional number is to store an approximate value."

"When performing narrowing conversions, we must explicitly tell the compiler that we haven't made an error: we are discarding part of the number on purpose. We use a cast operator (i.e. the type name in parentheses) to do this."

"This is how you should assign variables of different types:"

Java code Description
byte a = 115;
int b = a;
Widening primitive conversion. Everything's great.
int c = 10000;
byte d = (byte) c;
Narrowing primitive conversion. We must explicitly state that the extra bytes should be discarded.
int c = 10;
byte d = (byte) c;
Narrowing primitive conversion. We must explicitly state that the extra bytes should be discarded, even if they equal 0.
float f = 10000;
long l = (long) (f * f);
float f2 = l;
long l2 = (long) f2;
When assigning to a float, a widening primitive conversion takes place. When assigning a float to a long, a narrowing primitive conversion takes place. A cast operator is required.
double d = 1;
float f = (float) d;
long l = (long) f;
int i = (int) l;
short s = (short) i;
byte b = (byte) s;
Narrowing conversions in all assignment operations except for the first line. These conversions require that we explicitly indicate the type conversion.

"A cast operator must be put before a number/variable anytime part of the number will be discarded or when a narrowing primitive conversion occurs. The cast operator only affects the number/variable that directly follows it."

Java code Description
float f = 10000;
long l = (long) f * f;
Only one of the two variables is cast to a long: multiplication of a long and a float equals a float.
float f = 10000;
long l = (long) (f * f);
The entire expression is cast to a long.

"I see."