Arithmetic Assignment operators in Java


The five arithmetic assignment operators are a form of short hand. Various textbooks call them "compound assignment operators" or "combined assignment operators". Their usage can be explaned in terms of the assignment operator and the arithmetic operators.

Compound OperatorSample ExpressionExpanded Form
+= x+=2 x=x+2
-= y -=6 y=y-6
*= z *=7 z=z*7
/= a /=4 a=a/4
%= b %=9 b= b%9

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Source Code

public class Assignment
{
    public static void main(String args[])
    {
        int a=123;
        System.out.println(a);
        a+=10;
        System.out.println(a);
        a-=10;//a=a-10
        System.out.println(a);
        a*=10;
        System.out.println(a);
        a/=10;
        System.out.println(a);
        a%=10;
        System.out.println(a);
    }
}
 

Output

123
133
123
1230
123
3
To download raw file Click Here

Basic Programs