Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
 
 
Search All Tutorials

 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 
Java
  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. 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

                         

Facing Programming Problem?
Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

 

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.

Hot Web Programming Job

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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

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

Copyright © 2007. All rights reserved.