Home Java Master-java Simple Assignment Operator
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Simple Assignment Operator

Advertisement
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
The addition is: 15
The subtraction is: 5
The multiplication is: 50
The division is 5
The remainder is: 5
The result of AND operation : 0
The result of Bitwise inclusive OR operation : 10
The result of Signed left shift operation : 10240

Download this program

Advertisements

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: December 18, 2008

Ask Questions?    Discuss: Simple Assignment Operator   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mitali
October 21, 2011
Not satisfied with the output

If x -= y; x = (x - y); wher x = 5 & y = 10 then x = (x-y) x= (5-10) x= -5. I have executaed the program and the output is 5 too. Make me understand why it's not -5.
DMCA.com