Assignment Operators
Same for: C/ C++/ Java/ JavaScript/ VB/ PHP and others.
Assignment operators are used to assigning the value of an expression to a variable. We have seen the usual assignment operator, '='. In addition, java has a set of shorthand assignment operators which are used in the form
v op= exp;
where v is a varible, exp is an expression and op is a java binary operator. The operator op= is known as the shorthand assignment operator.
v op= exp;
is equivalent to
v = v op(exp);
with v accessed only once. consider an example:
x += y+1;
This is same as the statement
x = x+ (y+1);
The shorthand operator += means 'add y + 1 to x' or increment x by y+1 For y = 2. the above statement becomes
x += 3;
and when this statement is executed, 3 is added to x.
Statement with simple assignment operator | Statement with shorthand operator |
---|---|
a = a + 1 | a += 1 |
a = a - 1 | a -= 1 |
a = a * 1 | a * = 1 |
a = a / 1 | a /= 1 |
a = a % 1 | a %= 1 |
java tips thanks get the best java training in bay area
ReplyDeletePost a Comment