There are following Assignment Operators Supported by Java Language:
OPERATOR : =
DESCRIPTION :
Simple Assignment Operator, Assigns values from Right Side Operands to Left Side Operand
EXAMPLE :
C = A + B will Assign Value of A + B into C
OPERATOR : +=
DESCRIPTION :
Add AND Assignment Operator, It ADDS Right Operand to the Left Operand and Assign the Result to Left Operand
EXAMPLE :
C += A is Equivalent to C = C + A
OPERATOR : -=
DESCRIPTION :
Subtract AND Assignment Operator, It SUBTRACTS Right Operand from the Left Operand and assign the Result to Left Operand
EXAMPLE :
C -= A is Equivalent to C = C – A
OPERATOR : *=
DESCRIPTION :
Multiply AND Assignment Operator, It MULTIPLIES Right Operand with the Left Operand and Assign the Result to Left Operand
EXAMPLE :
C *= A is Equivalent to C = C * A
OPERATOR : /=
DESCRIPTION :
Divide AND Assignment Operator, It DIVIDES Left Operand with the Right Operand and Assign the Result to Left Operand
EXAMPLE :
C /= A is Equivalent to C = C / A
OPERATOR : %=
DESCRIPTION :
Modulus AND Assignment Operator, It takes MODULUS using Two Operands and Assign the Result to Left Operand
EXAMPLE :
C %= A is Equivalent to C = C % A
OPERATOR : <<=
DESCRIPTION :
Left Shift AND Assignment Operator
EXAMPLE :
C <<= 2 is SAME as C = C << 2
OPERATOR : >>=
DESCRIPTION :
Right Shift AND Assignment Operator
EXAMPLE :
C >>= 2 is SAME as C = C >> 2
OPERATOR : &=
DESCRIPTION :
Bitwise AND Assignment Operator
EXAMPLE :
C &= 2 is SAME as C = C & 2
OPERATOR : ^=
DESCRIPTION :
Bitwise Exclusive OR and Assignment Operator
EXAMPLE :
C ^= 2 is SAME as C = C ^ 2
OPERATOR : |=
DESCRIPTION :
Bitwise Inclusive OR and Assignment Operator
EXAMPLE :
C |= 2 is SAME as C = C | 2
Example Program :
public class Test
{
public static void main(String args[])
{
int a = 10;
int b = 20;
int c = 0;
c = a + b;
System.out.println(“c = a + b = ” + c );
c += a ;
System.out.println(“c += a = ” + c );
c -= a ;
System.out.println(“c -= a = ” + c );
c *= a ;
System.out.println(“c *= a = ” + c );
a = 10;
c = 15;
c /= a ;
System.out.println(“c /= a = ” + c );
a = 10;
c = 15;
%= a ;
System.out.println(“c %= a = ” + c );
c <<= 2 ;
System.out.println(“c <<= 2 = ” + c );
c >>= 2 ;
System.out.println(“c >>= 2 = ” + c );
c >>= 2 ;
System.out.println(“c >>= a = ” + c );
c &= a ;
System.out.println(“c &= 2 = ” + c );
c ^= a ;
System.out.println(“c ^= a = ” + c );
c |= a ;
System.out.println(“c |= a = ” + c );
}
}
Result:
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c >>= 2 = 1
c &= a = 0
c ^= a = 10
c |= a = 10
JAVA Tutorial PART 2 - Assignment Operators
0 comments:
Post a Comment