I am having troubling trying to figure out implicit promotions - since int and float are 32-bit, I assumed it'd be okay to assign one to another. It works for assigning an int to a float e.g.
But not the other way around, means when I try to assign a float to an int I get a compiler error -
Why should this be so since both, float and int are 32-bit??
Here is the order in ascending:
BYTE <SHORT <INT <LONG<FLOAT<DOUBLE.....
Smaller value casted implicitly....(check your first example: float is greater so assigning int value to float -OK)
But not the other way around.
If you want to assign float value to int do explicit cast.
int x = (int)ff; int is lower in the order and you are assigning higher element to lower one
posted 9 years ago
Thank You for the responses, I think I've got it now!
BYTE <SHORT <INT<LONG<FLOAT<DOUBLE.....
Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value.
Refer JLS 5.1.2
Farmers know to never drive a tractor near a honey locust tree. But a tiny ad is okay:
Two software engineers solve most of the world's problems in one K&R sized book