I have a doubt with this value assignment in java.
When I put this:
double[] sumMatematicas ={50};
int suma = 0;
suma += sumMatematicas[0]);
It doesn't throw a "cannot implicitly convert a double to an int " exception and returns 50 .
But if I write the assignment like this:
double[] sumMatematicas ={50};
int suma = 0;
suma =suma + sumMatematicas[0];
It throws me the "cannot implicitly convert a double to an int " exception.
Why does it work with +=
?
I answer this question based on the documentation link that appears in the link shared by @JFrame in the comments, since I found this question interesting and I did not want it to remain open.
The documentation for compound operators tells us the following:
What it tells us is that the compiler is simply casting the result as we see in the example in the same documentation:
That in the example you present we would have the following:
Equivalent to:
Thing that doesn't happen when you do:
Because there is no such cast and you are setting a double equal to an int which is causing the error you show.