47
I always thought the operator +=
functioned only as a shorter form for the traditional increment, for example:
i += j;
Instead of:
i = i + j;
But when performing the following experiment:
int i = 3;
long j = 7;
So when I execute i = i + j;
results in error, while i += j;
compiles normally.
Given this, is it possible to state that i += j;
is actually similar to i = (tipo do i) (i + j);
?
Only to the character of curiosity I tested in language
Objective-C
and worked the function that gave error in your code inJava
, I’ll dig deeper into that question.– iTSangar
@iTSangar I believe the error lies in mixing two types of data during the operation, so the need for a type casting:
i = (int) (i+j);
, I think...– Luiz Carvalho
I think it’s cool this kind of question because it demystifies things that people learn intuitively wrong. And intuition without a solid knowledge of the most damaging subject of help.
– Maniero