Possible Duplicate:
Varying behavior for possible loss of precision
I found an inconsistence in Java strong typing check at compile time. Please look at the following code:
int sum = 0;
sum = 1; //is is OK
sum = 0.56786; //compile error because of precision loss, and strong typing
sum = sum + 2; //it is OK
sum += 2; //it is OK
sum = sum + 0.56787; //compile error again because of automatic conversion into double, and possible precision loss
sum += 0.56787; //this line is does the same thing as the previous line, but it does not give us a compile error, and javac does not complain about precision loss etc.
Can anyone explain it to me? Is it a known bug, or desired behavior? C++ gives a warning, C# gives a compile error.
Does Java breaks strong typing? You can replace += with -= or *= - everything is acceptable by a compiler.
d
orf
) is a double, not a float. Besides, I think you might be missing the point.. – Memlingfloat f=1.0/10.0;
ordouble d=1.0f/10.0f;
? A good language should be able to define an==
operator so it behaves as an equivalence relation in all cases which compile (the fact thatx==y
compiles andy==z
compiles would not imply thatx==z
must compile, but if all three compile and two return true, the third should as well), but Java's fails in many regards. – Regiment