Arithmetic Assignment Operators


The following assignment operators are similar to the increment and decrement operators that we have just seen. They are used so that you do not need to enter the source variable twice when assigning a value to a variable based on its current value. The following table shows a list of arithmetic assignment operators for the arithmetic operators:

Operator Description
*= Multiplication assignment
/= Division assignment
+= Addition assignment
–= Subtraction assignment
%= Remainder assignment

So we can set a value to a variable and then double its current value as follows:

int number = 22;  
number *= 2;        
// all the fours, 44

In fact, it is possible to assign values to variables using the assignment operators wherever the value type is valid, even in mid-code, so to speak.

int numberA = 30;  
int numberB = 7;  
numberA /= numberB -= 4;

The last line of code first subtracts 4 from numberB, setting it to the value of 3. Then numberA, which equals 30, is divided by the new value of numberB, which now equals 3, giving numberA the value of 10, which is the result of 30 divided by 3. This conforms to the operator precedence table shown earlier.

Legal Disclaimer

Our website is not responsible for the information contained by this article. Webworldarticles.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.


This article was sent to us by: Gabriela C. Perez at 01032008

Related Articles

1. ISO Management Elements in Java EE .NET Platforms
In a heterogeneous application and platform environment, IT managers are faced with different and often incompatible management frameworks. IT organizations often partiti...

2. Importing Java Packages
To use a package within a Java application or applet, we need to import it. We do this by means of the import keyword. So, for example, if we wish to include the I/O packag...

3. What is a Java Package
A Java package is a collection of related classes that can be imported into your program to support your software. They also provide namespace management, as wel...

4. Character Escape Sequences
Character escape sequences allow for a character to be interpreted differently than its literal value. Character escape sequences are defined using the backslash (\) ch...

5. Conditional Statements
The ability to choose the path that your program takes, based on any given data, is the key to all functionality in programming. In order to create conditional statemen...

6. Java Methods
Methods are used as the building blocks of your program, performing tasks that can be called again and again and using the same code to perform the task each time. The basi...

7. Variable Scope
The scope of a variable is the area in which a variable belongs, specified by the area in which it is declared. The following example code contains two declared variables, ...