I was doing a program that tried to calculate pi, but java gave me an error:
Error: possible loss and conversion from double to int
I solved it by changing all my variables to the double type, but I was curious about the difference
I was doing a program that tried to calculate pi, but java gave me an error:
Error: possible loss and conversion from double to int
I solved it by changing all my variables to the double type, but I was curious about the difference
The type double
is a floating point data type, that is, it can store real numbers with decimals (up to a certain precision), such as 3.1415926537
or 1.0
, while that type int
is an integer type and can only store numbers without decimals, such as 1
, 2
, 3
, etc.
That's why by assigning a double
to a int
, java alerts you. The free translation of the message would be something like:
Error: possible conversion with information loss from double to int
More information at java Primitive Data Types (in English)