views:

80

answers:

2

I had an annoying issue, getting a "Possible loss of precision" error when compiling my Java program on BlueJ (But from what I read this isn't connected to a specific IDE).

I was surprised by the fact that the compiler told me there is a possible loss of precision and wouldn't let me compile/run the program. Why is this an error and not a warning saying you might lose precision here, if you don't want that change your code?

The program runs just fine when I drop the float values; it wouldn't matter since there is no point (e.g [143.08, 475.015]) on my screen.

On the other hand when I loop through an ArrayList and in this loop I have an if clause removing elements from the ArrayList it runs fine, just throws an error and doesn't display the ArrayList [used for drawing circles] for a fraction of a second. This appears to me as a severe error but doesn't cause (hardly) any troubles, while I wouldn't want to have such a thing in my code at all.

What's the boundary?

+1  A: 

It's an error because the spec doesn't allow narrowing primitive conversions in an Assignment Conversion.

If you really want to assign a larger datatype to a smaller one, such as double to float, you must explicitly cast it.

R. Bemrose
Yes, good, that's why it's an error in the compiler -- because the compiler implements the language and the language says this is illegal. I suppose the bug is in the error message: "illegal narrowing conversion in assignment" is more precise but maybe just as inscrutable...
Sean Owen
A: 

It could lead to computation errors that are very hard to detect. So it's right that it is error - you do not want a program to give you result with unknown error in result.

binary_runner