In this section you will learn about the Assignment Operators. This is one type of operators.
Assignment Operators :
This operator is used whenever you need to assign some value to your variables. You can assign some value to the variable or also you can assign specified values to the variable by using any assignment operator. Your variables are placed left side of your assignment operator and value is putted right side of the assignment operator. At the time of evaluation you flow from right to left.
= (Simple assignment operator)  This is simple assignment operator Which assigns value of right side operands to the left side operand.
eg  Z= X+Y This assigns the value of X+Y to the variable Z.
+=(Add AND assignment operator)  This operator adds the right operand to the left one and assigns that to the left operand.
eg Y+=X; which shows Y=Y+X;
= (Subtract AND assignment operator)  This operator subtracts the right operand to the left one and assigns that to the left operand.
eg Y=X; which shows Y=YX;
*= (Multiply AND assignment operator)  This operator multiplies the right operand to the left one and assigns that to the left operand.
eg Y*=X; which shows Y=Y*X;
/= (Divide AND assignment operator)  This operator divides the left operand by the right one and assigns that to the left operand.
eg Y/=X; which shows Y=Y/X;
%= (Modulus AND assignment operator)  This operator takes modulus of the left operands by the right operand and save the result to the left operand.
eg Y%=X; which shows Y=Y%X;
<<= this operator is used for Left shift and called as Left shift AND assignment operator.
eg. Y<<3 which shows Y=Y<<3
>>= this operator is used for Right shift and called as Right shift AND assignment operator.
eg. Y>>=3 which shows Y=Y>>3
&= This is Bitwise AND assignment operator
eg. Y&=3 which shows Y=Y&3
^= This is bitwise exclusive OR and assignment operator.
eg. Y^=3 which shows Y=Y^3
= This is bitwise inclusive OR and assignment operator.
eg. Y=3 which shows Y=Y3
Example :
package operator; class AssignmentOperator{ public static void main(String[] args){ int x = 100; int y = 200; int z; z = x+y; System.out.println("z = x+y : " + z ); z += x ; System.out.println("z += x : " + z ); z = x ; System.out.println("z = x : " + z ); z *= x ; System.out.println("z *= x : " + z ); x=20; y=100; y /= x ; System.out.println("y /= x :" + y ); x=15; y=100; y %= x ; System.out.println("y %= x : " + y ); x <<= 2 ; System.out.println("x <<= 2 : " + x ); y >>=2 ; System.out.println("y >>= 2 : " + y ); y &= x ; System.out.println("y &= x : " + y ); y ^= x ; System.out.println("y ^= x : " + y ); y = x ; System.out.println("y = x : " + y ); } }
Output :
z = x+y : 300 z += x : 400 z = x : 300 z *= x : 30000 y /= x :5 y %= x : 10 x <<= 2 : 60 y >>= 2 : 2 y &= x : 0 y ^= x : 60 y = x : 60
If you enjoyed this post then why not add us on Google+? Add us to your Circles
Liked it! Share this Tutorial

Discuss: Assignment Operators in java 7
Post your Comment