|
Emphasis:
|
|
Most of the integer (arithmetic) operators should look familiar to you...
(Except for the % operator)
Operator symbol | Operation | Note |
---|---|---|
+ | addition | Binary operator, e.g.: 9 + 4 = 13 |
− | subtraction | Binary operator, e.g.: 9 − 4 = 5 |
* | multiplication | Binary operator, e.g.: 9 * 4 = 36 |
/ | division (= quotient) | Binary operator, e.g.: 9 / 4 = 2 |
% | modulus (= remainder) | Binary operator, e.g.: 9 % 4 = 1 |
( ... ) | brackets | Changes order of computation |
− | negation |
Changes the sign of the value:
− 4 = (−4)
(Read as: − 4 = negative 4) |
![]() |
The / integer operator will always produce an integer result
Examples:
9 / 4 = (floating point result = 2.25) = 2 -9 / 4 = (floating point result = -2.25) = -2 9 / -4 = (floating point result = -2.25) = -2 -9 / -4 = (floating point result = 2.25) = 2 |
The sign of the result is always equal to the sign of the dividend
Examples:
9 % 4 = (floating point result = 2.25) = 1 -9 % 4 = (floating point result = -2.25) = -1 9 % -4 = (floating point result = -2.25) = 1 -9 % -4 = (floating point result = 2.25) = -1 |
dividend = quotient × divisor + remainder |
Example:
9 / 4 = 2 9 % 4 = 1 9 = 2 * 4 + 1 -9 / 4 = -2 -9 % 4 = -1 -9 = (-2)* 4 + (-1) 9 / -4 = -2 9 % -4 = 1 9 = (-2)*(-4) + 1 -9 / -4 = 2 -9 % -4 = -1 -9 = 2 *(-4) + (-1) |
|
Special emphasis:
|
|
Operator | Priority | Note |
---|---|---|
( .... ) | Highest | |
− (negation) | Higher | Unary operator, e.g.: −3 |
* / % | High | Binary operator, e.g.: 4 * 5 |
+ − | Lowest | Binary operator, e.g.: 4 + 5 |
Operator | Associativity | Example |
---|---|---|
( .... ) | "inside out" | ((3 + 4) * 5 ) = (7 * 5) |
− (negation) | Right | − − 3 = − (−3) = 3 (from right to left) |
* / % | Left | 4 * 5 % 7 / 4 = 20 % 7 / 4 = 6 / 4 = 1 |
+ − | Left | 4 − 6 + 4 − 7 = (−2) + 4 − 7 = 2 − 7 = −5 |
Integer expression: 72 / 10 + 72 % 10 Evaluated as follows: 72 / 10 + 72 % 10 = 7 + 72 % 10 = 7 + 2 = 9 |
(This is how you compute the sum of the digits in the number 72 !!!)
Integer expression: 22 - - 3 * - 4 + - - 1 Evaluated as follows: 22 - - 3 * - 4 + - - 1 = 22 - (-3) * - 4 + - - 1 = 22 - (-3) * (-4) + - - 1 = 22 - (-3) * (-4) + - (-1) = 22 - (-3) * (-4) + (+1) = 22 - 12 + 1 (Use associativity rule) = 10 + 1 = 11 |
Integer expression: (22 - - 3) * - (4 + - - 1) Evaluated as follows: (22 - - 3) * - (4 + - - 1) = (22 - (-3)) * - (4 + - - 1) = (22 - (-3)) * - (4 + - (-1)) = (22 - (-3)) * - (4 + 1) = 25 * - (4 + 1) = 25 * - 5 = 25 * (-5) = -125 |
How to run the program:
|
public class Exercise1 { public static void main(String[] args) { int a = 15; int b = 24; System.out.println(b - a + 7); System.out.println(b - a - 4); System.out.println(b % a / 2); System.out.println(b % (a / 2)); System.out.println(b * a / 2); System.out.println(b * (a / 2)); System.out.println(b / 2 * a); System.out.println(b / (2 * a)); } } |
16 5 4 3 180 168 180 0 |
How to run the program:
|
|
12 is divisible by 2 ⇔ 12 % 2 = 0 12 is divisible by 3 ⇔ 12 % 3 = 0 12 is divisible by 4 ⇔ 12 % 4 = 0 12 is not divisible by 5 ⇔ 12 % 5 = 2 12 is divisible by 6 ⇔ 12 % 6 = 0 12 is not divisible by 7 ⇔ 12 % 7 = 5 |