开发者

Warning vs. error

开发者 https://www.devze.com 2022-12-30 20:12 出处:网络
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 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开发者_Python百科 I wouldn't want to have such a thing in my code at all.

What's the boundary?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消