Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Simple Assignment Operator 
 

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

                         

» View all related tutorials
Related Tags: c class static io classes instance with member delegation sse ci explicit e it ls can li use ce in

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.