Java-Arithmetic Compound Assignment Operators

Arithmetic Compound Assignment Operators

Java provides special operators that can be used to combine an arithmetic operation with an assignment. As you probably know, statements like the following are quite common in programming :

In Java, you can rewrite this statement as shown here :

This version uses the += compound assignment operator. Both statements perform the same action, they increase the value of ‘mark’ by 10.

Program

0 (1) - Copy

Program Source

class Javaapp {

    public static void main(String[] args) {
        
        int a = 5;
        int b = 6;
        int c = 7;
        int d = 8;
        int e = 13;
        a+=10;
        b-=4;
        c*=5;
        d/=4;
        e%=10;
        System.out.println("a = "+a);
        System.out.println("b = "+b);
        System.out.println("c = "+c);
        System.out.println("d = "+d);
        System.out.println("e = "+e);
    }
}

Leave a Comment