NoCastIfSameSize
DRAFT

Misconception:

One can assign a value to a variable without needing any explicit cast if the variable’s type’s size is at least as big (in terms of the number of bits) as the value’s type’s size (e.g., a float value can be assigned to an int variable without a cast).

Incorrect

If a variable is at least as big (bit-width) as a value, then no cast is needed to a assign the value to the variable

Correct

Correction
Here is what's right.

While the size is a relevant aspect when deciding whether or not a cast is needed, it is not the only aspect. It is not possible to assign a float to an int (even though both are 32 bits wide), or a double to a long (both 64 bits) without a cast.

Floating-point numbers have a much larger range than the integral numbers, and thus trying to store them into an integral variable might lead to a dramatically different value.

int i = 3F; // wrong
double d = 3D; // wrong

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.