Java Reference
In-Depth Information
The compound assignment operators
Javacontainsseveralcompoundoperatorsthatweredesignedtomake
codemorecompact.Thecompoundassignmentoperatorsconsistofacom-
binationofthesimpleassignmentoperator(=)withanarithmeticor
bitwiseoperator.Forexample,toadd5tothevariable y wecancode
y=y+5;
Alternatively,wecancombinetheadditionoperator(+)withthesim-
pleassignmentoperator(=)asfollows
y+=5;
Ineithercasethefinalvalueof y isitsinitialvalueplus5,butthelatter
formreducesthesizeoftheprogram. Table8-4 liststhecompoundas-
signment operators.
Table 8-4
Java Compound Assignment Operators
OPERATOR
ACTION
+=
addition assignment
-=
subtraction assignment
*=
multiplication assignment
/=
division assignment
%=
remainder assignment
&=
bitwise AND assignment
|=
bitwise OR assignment
^=
bitwise XOR assignment
<=
left shift assignment
>=
right shift assignment
>>=
compound right shift assignment
Programmers note:
In compound assignments, the = sign always comes last.
Note that the compound assignment is not available for the NOT (~)
bitwise unary operator or for the unary increment (++) or decrement (—)
operators. The explanation is that unary (one element) statements do not
require simple assignment; therefore, the compound assignment form is
meaningless.
 
Search WWH ::




Custom Search