Basic Java Language: Difference between revisions

wikademia>Lurkingfate
→Equals: Merging section with Assignment Operator.
wikademia>Lurkingfate
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 ===
=== Addition '+', Subtraction '-', multiplication '*', division '/', and modulus '%' operators ===
Just like the addition operator, the subtraction, multiplication, division, and modulus operators are used as follows:
The addition, subtraction, multiplication, division, and modulus operators are used as follows:
<pre>
<pre>
int a = 9;
int a = 9;
 
a+1; // evaluates to 10
a-1; // evaluates to 8
a-2; // evaluates to 7
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.
You have seen addition aubtraction, multiplication, and division 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: