Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: 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 Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Java Notes: Extended Assignment Operators

 

In this tutorial you will learn how to use the extended assignment operators.

 

It's very common to see statement like the following, where you're adding something to a variable.

 

sum = sum + currentValue;
i = i + 2;

 

A shortcut way to write assignments like this is to use the += operator.

 

It's one operator symbol so don't put blanks between the + and =.

 

With this notation it isn't necessary to repeat the variable that is being assigned to.

 

// Same as "sum = sum + currentValue"
sum += currentValue;   

// Same as "i = i + 2"
i += 2;                

Commonly used with arithmetic operators

You can use this style with all arithmetic operators (+, -, *, /, and even %).

measurementRange /= 2;         // Divides measurementRange by 2.
accountBalance -= withdrawal;  // Subtracts withdrawal from accountBalance.

Four ways to add one

There are four ways to add one to a variable. Many languages only support the first way. Current compilers translate them into equivalent code, so there's no efficiency difference, unlike early C compilers where ++ was more efficient.

i = i + 1;        // Common in all languages.
i += 1;           // Common when adding values other than one.
i++;              // Most common, but only works for adding one.
++i;              // Least common.

Can be used with most computational operators

This notation can also be used with the bit operators (^, &, |). It can not be used with the logical short-circuit operators && and ||, but you can use the & and | versions. It can only be used with binary (two operand) operators, not unary operators.

Where it makes a difference

For simple variables, which kind of assignment to use is mainly a style or readability difference. However, when assigning to l-values that require computation, there is a real difference, both in performance and possible side-effects.

a[f(x)] = a[f(x)] + 1;   // Calls f(x) twice!
a[f(x)] += 1;            // Calls f(x) only once.
Ask programming questions?

 

 

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:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
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.

[an error occurred while processing this directive]