Arithmetic operators
Lesson content
- Arithmetic operators in Java
- Operator precedence
- Most common errors
Arithmetic operators in Java
In the Java programming language, it is possible to write various arithmetic expressions. Some of the previous examples already include the use of the addition and subtraction operators, but the complete list of arithmetic operators can be seen in the following table.
| Operator | Description | Examples |
|---|---|---|
| + | Addition | a + b; 12 + 5; m1.cubicVolume + 300; |
| - | Subtraction | a - b; 12 - 5; m1.cubicVolume - 300; |
| * | Multiplication | a * b; 12 * 5; m1.cubicVolume * 300; |
| / (integers) | Integer Division (DIV) | 2 / 12; a / b; |
| / (real numbers) | Division | a / 12.0; 5.0 / 123.44; |
| % | Remainder of integer division (MOD) | 5 % 2; a % 10; |
| ++ | Increment by 1 (increment) | a++; m1.cubicVolume++; |
| — | Decrement by 1 (decrement) | a—; m1.cubicVolume—; |
| += | Increment the value of the same variable | a += 3; |
| -= | Decrement the value of the same variable | a -= 3; |
| *= | Multiply the value of the same variable | a *= 3; |
| /= | Divide the value of the same variable | a /= 3; |
| %= | Remainder of dividing the same variable | a %= 3; |
The addition, multiplication, and subtraction operators are used in a similar way as they would be in mathematics. These operators can be used with both integer and real values and variables. Java also allows the use of parentheses, so expressions like this are common:
1 b * 2 * ((234.55 + 20) - (a * 3.4))
Increment and decrement operators increase or decrease the value of an integer variable by one. They provide a shorthand for certain arithmetic expressions, so the following two sets of expressions are entirely equivalent:
1 a = a + 1;
2 b = b - 1;
3
4 // Is the same as...
5
6 a++;
7 b--;
Operators +=, -=, *=, /=, and %= were introduced to allow for shorter notation when new values of variables are calculated based on their existing values. Therefore, the following two sets of expressions are equivalent:
1 a = a + 3;
2 a = a - 4;
3 a = a * 5;
4 a = a / 6;
5 a = a % 7;
6
7 // Is the same as...
8
9 a += 3;
10 a -= 4;
11 a *= 5;
12 a /= 6;
13 a %= 7;
The division operators in Java are slightly more complex than they may initially seem. First, the operator for real number division and the operator for integer division are both represented by the same symbol - the forward slash (/).
Real number division is “normal division” (e.g., 11.0 / 3 = 3.66666).
Integer division, on the other hand, represents division without remainder (e.g., 11 / 3 = 3). In this case, the decimal part (the part after the decimal point 0.66666) is discarded, and the integer part is returned as the result (3).
Since both operators are written the same way, the question arises as to how the Java compiler distinguishes when to use one and when to use the other. The answer is quite simple – it checks whether the operands (numbers or variables being divided) are integers or real numbers, and applies the appropriate operator.
If both numbers are integers, integer division is used.
If at least one of the numbers is real, real number division is used.
Additionally, division by zero is not allowed, so the following expressions would cause an error:
1 int a = 55 / 0;
2 double b = 123.45 / 0.0;
3
4 int c = 0;
5 int d = 54 / c;
The operator for calculating the remainder of integer division can only be used with integer values. The result represents the whole number that remains as the remainder when two integers are divided. The division and remainder operators are best explained through a few simple examples.
Example 1
The following expressions in Java will be interpreted as real number division expressions (class ArithmeticExpressions):
1 class ArithmeticExpressions {
2
3 public static void main(String[] args) {
4 double x = 3.5;
5 double y = 7;
6 double z;
7
8 z = y / x;
9 //The result is 2.0
10 //Both x and y are decimal numbers.
11
12 z = y / 7;
13 //The result is 1.0
14 //It is sufficient if just one operand is a real number - y
15
16 z = 35.0 / 7;
17 //The result is 5.0
18 //It is sufficient if just one operand is a real number - 35.0
19
20 z = 35 / 7.0;
21 //The result is 5.0
22 //It is sufficient if just one operand is a real number - 7.0
23
24 //However, the following expressions will be evaluated as integer
25 //division,
26 int a = 2;
27 int b = 12;
28 int c;
29 double r;
30
31 c = b / a;
32 //The result is 6 (not 6.0 but just 6)
33 //Both a and b are integer variables
34
35 c = b / 4;
36 ////The result is 3
37 //b is an integer variable
38
39 c = 123 / 12;
40 ////The result is 10 (the decimal part is omitted)
41 //Both 123 and 12 are integer numbers
42
43 r = 123 / 12;
44 //The result is 10.0.
45 //Both 123 and 12 are integer numbers, so the first operation
46 // is integer division, giving the result 10, which is then
47 // transformed into a real number value 10.0 and stored in
48 // the double variable r.
49 }
50 }
As seen in the last example, it is possible for unexpected results to occur during division if the wrong operator is used. The cause is always the incorrect use of the integer division operator instead of the real number division operator.
To ensure the previous expression is interpreted as real number division, it needs to be written in one of the following ways:
1 r = 123.0 / 12;
2 r = 123.0 / 12.0;
3 r = 123 / 12.0;
4
5 // The result is 10.25
6 // It’s important that at least one of the numbers is real
Here are a few examples using the operator for calculating the remainder of integer division:
1 c = 5 % 2;
2 // The result is 1 (because 5 = 2 * 2 + 1)
3
4 c = 10 % 2;
5 // The result is 0 (because 10 = 5 * 2 with no remainder)
6
7 c = 14 % 4;
8 // The result is 2 (because 14 = 3 * 4 + 2)
9
10 c = 55 % 5;
11 // The result is 0 (because 55 = 11 * 5 with no remainder)
This operator can be used to check if a number is even, as the remainder when divided by two will always be zero if the number is even and one if it is odd. Similarly, it can check divisibility by other numbers (the remainder is zero if the number is divisible and non-zero otherwise).
Operator precedence
Finally, as in mathematics, not all arithmetic operators in Java have the same precedence (the order in which they are evaluated in an expression). Java has operator precedence rules which define that:
- Increment (++) and decrement (—) operators have higher precedence than all other operators.
- Multiplication (*), division (/), and remainder (%) have higher precedence than addition (+) and subtraction (-).
- In an expression where parentheses have been used, the part of the expression within the innermost parentheses will be evaluated first, and so on.
- In an expression where all operators that have been used have the same precedence and no parentheses have been used, the expression will be evaluated left to right.
In other words, if parentheses are not used in an arithmetic expression, operator precedence will be followed, for example:
1 c = 5 + 3 * 5;
2 // The result is 20
3
4 c = (5 + 3) * 5;
5 // The result is 40
6
7 c = 2 * 8 / 8 + 8;
8 // Result is 10
9
10 c = 2 * 8 / (8 + 8);
11 // Result is 1
Most common errors
Some of the most common syntax errors related to using arithmetic operators and the assignment operator are:
- The variable on the left side and the expression on the right side of the assignment operator are not of the same type, e.g.:
1 int number = 12.3;
2 int result = 35.4 - 2;
3 int result2 = m.multiply(154.3, 122.02);
instead of (correct):
1 double number = 12.3;
2 double result = 35.4 - 2;
3 double result2 = m.multiply(154.3, 122.02);
- Division by zero (zero is the divisor):
1 34 / 0;
Other common mistakes that are not syntax errors, but affect program behavior:
- Incorrectly using integer division instead of real number division. This often results in (unexpected) zero:
1 // The result is zero, not 0.5
2 12 / 24
3
4 // The percentage of girls will be 0.0, not 50.0
5 int numberOfStudents = 30;
6 int numberOfGirls = 15;
7 double percentageOfGirls = numberOfGirls / numberOfStudents * 100.0;
instead of:
1 // The result is 0.5 in all variations
2 12.0 / 24
3 12 / 24.0
4 12.0 / 24.0
5
6 // The percentage of girls will be 50.0
7 int numberOfStudents = 30;
8 int numberOfGirls = 15;
9 double percentageOfGirls = (numberOfGirls * 100.0) / numberOfStudents;