Home Java Java - Assignment Operators
Java - Assignment Operators
Java assignment operators are used to assign values to variables. These operators modify
the value of a variable based on the operation performed. The most commonly used
assignment operator is =, but Java provides multiple compound assignment operators for
shorthand operations.
List of Java Assignment Operators
The following table lists the assignment operators in Java:
Operator Description Example
Simple assignment operator. Assigns values from C = A + B will assign value of
=
right side operands to left side operand. A + B into C
Add AND assignment operator. It adds right
C += A is equivalent to C = C
+= operand to the left operand and assign the result
+ A
to left operand.
Subtract AND assignment operator. It subtracts
-= right operand from the left operand and assign C -= A is equivalent to C = C − A
the result to left operand.
Multiply AND assignment operator. It multiplies
C *= A is equivalent to C = C
*= right operand with the left operand and assign
* A
the result to left operand.
Divide AND assignment operator. It divides left
/= operand with the right operand and assign the C /= A is equivalent to C = C / A
result to left operand.
Modulus AND assignment operator. It takes
%= modulus using two operands and assign the C %= A is equivalent to C = C % A
result to left operand.
<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2
>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2
&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2
^= bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2
|= bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2
The following programs are simple examples which demonstrate the assignment
operators. Copy and paste the following Java programs as [Link] file, and compile and
run the programs −
Example 1
In this example, we're creating three variables a,b and c and using assignment
operators. We've performed simple assignment, addition AND assignment, subtraction
AND assignment and multiplication AND assignment operations and printed the results.
public class Test {
public static void main(String args[]) {
int a = 10;
int b = 20;
int c = 0;
c = a + b;
[Link]("c = a + b = " + c );
c += a ;
[Link]("c += a = " + c );
c -= a ;
[Link]("c -= a = " + c );
c *= a ;
[Link]("c *= a = " + c );
}
}
Output
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
Example 2
In this example, we're creating two variables a and c and using assignment operators.
We've performed Divide AND assignment, Multiply AND assignment, Modulus AND
assignment, bitwise exclusive OR AND assignment, OR AND assignment operations and
printed the results.
public class Test {
public static void main(String args[]) {
int a = 10;
int c = 15;
c /= a ;
[Link]("c /= a = " + c );
c = 15;
c %= a ;
[Link]("c %= a = " + c );
c = 15;
c &= a ;
[Link]("c &= a = " + c );
c = 15;
c ^= a ;
[Link]("c ^= a = " + c );
c = 15;
c |= a ;
[Link]("c |= a = " + c );
}
}
Output
c /= a = 1
c %= a = 5
c &= a = 10
c ^= a = 5
c |= a = 15
Example 3
In this example, we're creating two variables a and c and using assignment operators.
We've performed Left shift AND assignment, Right shift AND assignment, operations and
printed the results.
public class Test {
public static void main(String args[]) {
int a = 10;
int c = 0;
c <<= 2 ;
[Link]("c <<= 2 = " + c );
c = 15;
c >>= 2 ;
[Link]("c >>= 2 = " + c );
}
}
Output
c <<= 2 = 0
c >>= 2 = 3