I have seen this symbol/operator in a block of code:
a+=1;
But I cannot figure out what it does. Can someone help me please?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
From the Java Language Specification:
The last phrase is important if the left-hand side has side effects:
This is not equivalent to:
The first expression will increment
ionce. The second will incrementitwice and will assign the right-hand value to a different element ofarraythan will the first expression.I should note that these kind of side-effect statements are not good programming form, despite the fact that you often find them used.
The cast is also important because the type of
(E1) op (E2)may not be assignment-compatible withE1. For example, ifais of typeshort, thena++is not equivalent toa = a + 1. The latter will not compile because the type ofa + 1isintand cannot be assigned to ashortvariable without a cast. That’s why the spec in this case says thata++is equivalent toa = (short) ((a) + (1)). The same thing goes ifais of typecharorbyte.