Compound Assignment Operator Example Program in Java
A Program to show the use of Compound Assignment Operator
The program shows a few compound assignment operators and how it can be used in programs to write program intelligently. This helps to reduce a few lines while writing a program. It is also efficient than normal coding.
Program:
// Demonstrate compound assignment operators.
public class CompoundAssignemntOperators {
public static void main(String[] args) {
int x = 1;
int y = 3;
int z = 5;
x += 2; //Equals to x=x+2; Now x=3;
y *= 4; //Equals to y=y*4; Now y=12;
z += x * y; // z= z+(x*y); Now z=41 i.e z=5+(3*12)
z %= 6; // z= z%6; Now z=5; i.e z=41%6
System.out.println("x = " +
x);
System.out.println("y = " +
y);
System.out.println("z = " +
z);
}
}
Output:
x = 3
y = 12
z = 5