Basic Java Language: Difference between revisions
wikademia>Lurkingfate |
wikademia>Lurkingfate Undo revision 386490 by Lurkingfate (Talk) |
||
| Line 172: | Line 172: | ||
Note, this operator is not to be confused with the concatenation operator, which we will discuss later. | Note, this operator is not to be confused with the concatenation operator, which we will discuss later. | ||
=== | === Subtraction '-', multiplication '*', division '/', and modulus '%' operators === | ||
Just like the addition operator, the subtraction, multiplication, division, and modulus operators are used as follows: | |||
<pre> | <pre> | ||
int a = 9; | int a = 9; | ||
a- | a-1; // evaluates to 8 | ||
a/3; // evaluates to 3 | a/3; // evaluates to 3 | ||
a*2; // evaluates to 18 | a*2; // evaluates to 18 | ||
| Line 183: | Line 183: | ||
</pre> | </pre> | ||
Subtraction, multiplication, and division, you have seen before, but the modulus operator is much less commonly used. Actually, it is commonly used, but it known under a different name, the remainder. Nine divided by four gives a remainder of one, so therefore 9%4 (pronounced "9 mod 4") evaluates to 1. | |||
When more than one of the operators are used in the same statement, for example: | When more than one of the operators are used in the same statement, for example: | ||