Statements like the following
a = a + 4;
can be rewritten as
a += 4;
Both statements perform the same action: they increase the value of a by 4.
Here is another example,
a = a % 2;
which can be expressed as
a %= 2;
In this case, the %= obtains the remainder of a/2 and puts that result back into a.
Any statement of the form
var = var op expression;
can be rewritten as
var op= expression;
Here is a sample program that shows several op= operator assignments:
public class Main {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3;
a += 1;
b *= 2;
c += a * b;
c %= 3;
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
}
}
The output of this program is shown here:
a = 2
b = 4
c = 2
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |