Assignment operator is the most common operator almost used with all programming languages.
Simple Assignment Operator
Assignment operator is the most common operator almost
used with all programming languages. It is represented by "=
" symbol in Java which is used to
assign a value to a variable lying to the left side of the assignment operator. But, If the
value already exists in that variable then it will be overwritten by the
assignment operator (=). This operator can also be used to assign the
references to the objects. Syntax of using the assignment operator
is:
<variable> = <expression>; |
For example:
int counter = 1; String name = "Nisha"; boolean rs = true; Shape s1 = new Shape(); // creates new object Shape s2 = s1; //assigning the reference of s1 to s2 counter = 5; // previous value is overwritten |
In all cases a value of right side is being assigned to its type of variable lying to the left side. You can also assign a value to the more than one variable simultaneously. For example, see these expressions shown as:
x = y = z = 2;
x =(y + z);
Where the assignment operator is evaluated from right to left. In the first
expression, value 2 is assigned to the variables "z", then
"z" to "y", then "y"
to "x" together. While in
second expression, the evaluated value of the addition operation is assigned to
the variable "x" initially then the value of variable "x" is
returned.
Apart from "=" operator, different kind of assignment operators
available in Java that are know as compound assignment operators and can
be used with all arithmetic or, bitwise and bit shift operators.
Syntax of using the compound assignment operator is:
operand operation= operand
In this type of expression, firstly an arithmetic
operation is performed then the evaluated value is assigned to a left most
variable. For example an expression as x
+= y; is equivalent to the expression as x
= x + y; which adds the value of operands "x" and
"y" then stores back to the variable
"x".
In this case, both variables must be of the same type.
The table shows all compound assignment operators which you can use to make your code more readable and efficient.
Operator | Example | Equivalent Expression |
+= | x += y; | x = (x + y); |
-= | x -= y; | x = (x - y); |
*= | x *= y; | x = (x * y); |
/= | x /= y; | x = (x / y); |
%= | x %= y; | x = (x % y); |
&= | x &= y; | x = (x & y); |
|= | x != y; | x = (x ! y); |
^= | x ^= y; | x = (x ^ y); |
<<= | x <<= y; | x = (x << y); |
>>= | x >>= y; | x = (x >> y); |
>>>= | x >>>= y; | x = (x >>> y); |
Lets have an example implementing some compound
assignment operators:
class CompAssignDemo{ public static void main(String[] args) { int x=5; int y=10; x += y; System.out.println("The addition is:"+ x); x -= y; System.out.println("The subtraction is:"+ x); x *= y; System.out.println("The multiplication is:"+ x); x /= y; System.out.println("The division is"+ x); x %= y; System.out.println("The remainder is:"+x); x &= y; System.out.println("The result of AND operation :"+ x); x |= y; System.out.println("The result of Bitwise inclusive OR operation :"+ x); x <<= y; System.out.println("The result of Signed left shift operation :"+ x); } }
Output of the Program:
C:\nisha>javac CompAssignDemo.java
C:\nisha>java CompAssignDemo |