javacodex.com
Java Examples
Java Examples
memu home questions

Compound Assignment Operators

Compound assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on the two operands before assigning the result to the first operand.

Source: (CompoundAssignment.java)

public class CompoundAssignment {
   public static void main(String[] args) {
      int a = 10;
      int b = 10;
      int c = 10;
      int d = 10;
 
      a -= 2;
      b += 2;
      c /= 2;
      d *= 2;
 
      System.out.println("a = " + a );
      System.out.println("b = " + b );
      System.out.println("c = " + c );
      System.out.println("d = " + d );
   }
}
 

Output:

# java CompoundAssignment 
a = 8
b = 12
c = 5
d = 20

Contact: javacodex@yahoo.com