posted 20 years ago
Hi Tyler,
The way to tell the compiler "I know, but it's OK" is to use a cast. This will give you a "posible loss of precision" type of error:
int x = 0;
double d = 1;
x = d;
Now, you say to yourself "That's silly, I know d is 1, and so this is going to be just fine". To tell the compiler that it's going to be fine, you "cast d to an int:"
x = (int) d;
d is then converted to an int before the assignment is made. Any actual loss of precision that occurs (i.e., overflow) is ignored.
Casts are useful in many other contexts as well -- maybe you've already used them with objects, but not with primitives in this way.