Wednesday, 25 April 2018

Core Java: Compound Assignment Operators

Besides the usual simple assignment operator (=) described earlier, Java also provides the so-called compound assignment operators as listed:
 
Operation
Description
Usage
Example
=
Assignment
Assign the value of the LHS to the variable at the RHS
var = expr
x = 5;
+=
Compound addition and assignment
var += expr
same as var = var + expr
x += 5;
same as x = x + 5
-=
Compound subtraction and assignment
var -= expr
same as var = var - expr
x -= 5;
same as x = x - 5
*=
Compound multiplication and assignment
var *= expr
same as var = var * expr
x *= 5;
same as x = x * 5
/=
Compound division and assignment
var /= expr
same as var = var / expr
x /= 5;
same as x = x / 5
%=
Compound remainder (modulus) and assignment
var %= expr
same as var = var % expr
x %= 5;
same as x = x % 5

No comments:

Post a Comment

Please write your view and suggestion....