Compound Assignment Operators in Java

The compound assignment operator(+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=,>>>=) is a combination of the assignment operator and another operator such as an arithmetic operator. So they are also known as Shorthand assignment operators.

Syntax:

  Variable operator variable or constant

The following Table shows the compound assignment operators and their usage

Operator                    Expression(shorthand)                   meaning

+=                             vall+=increasee;                            vall=vall+increasee;

-=                             firstt-=25;                                          firstt=firstt-25;

/=                             firstt/=sec;                                         firstt=firstt/sec;

*=                            costt*=qtty+1;                                costt=costt*(qtty+1);

%=                          FIRR%=SECC;                                         FIRR=FIRR%SECC;

&=                          firstt&=seec;                                         firstt=firstt&seec;

|=                           firstt|=seec;                                          firstt=firstt|seec;

^=                           firstt^=seec;                                          firstt=firstt^seec;

<<=                        cpricee<<=spricee;                            cpricee=cpricee<<spricee;

>>=                        firstt>>=seec;                                         firstt=firstt>>seec;

>>>=                     biib>>>=niib;                                       biib=biib>>>niib;

Compound Assignment Operators with example

class CompoundAssignment

{

public static void main(String aa[])

{

int a,b,c;

a=1;

b=2;

c=3;

a+=5;

b*=4;

c+=a*b;

c%=6;

System.out.println(“a=”+a);

System.out.println(“b=”+b);

System.out.println(“c=”+c);

System.out.println(“c%=”+c);

}

}

Compound Assignment Operators in Java