Opening the reading…
Opening the reading…
PROGRAMMING FUNDAMENTALS › JAVA FUNDAMENTALS
With a = 17 and b = 5, each binary arithmetic operator combines the same two int values in a different way. Keep the calculations separate so each result is easy to identify. None of these operations changes a or b, so both variables still hold their original values after every statement.
int a = 17;
int b = 5;
System.out.println(a + b); // 22
System.out.println(a - b); // 12
System.out.println(a * b); // 85
System.out.println(a / b); // 3
System.out.println(a % b); // 2The plus operator adds, minus subtracts, multiplication repeats a value by a factor, division finds the quotient, and remainder finds what is left after complete groups are removed. The last two results need special attention because both operands are int values. Java performs integer division here, so the result is not a decimal value.
What does this separate statement print when a = 17 and b = 5? System.out.println(a % b);
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
For 17 / 5, you can make three complete groups of five. Those groups use 15 of the 17 counters, so the integer quotient is 3. The two counters not placed in a complete group are the remainder, which is why 17 % 5 produces 2.
When both operands are int values, Java discards the fractional part of the quotient rather than rounding it. Mathematically, 17 divided by 5 is 3.4, but the Java expression 17 / 5 has the int result 3. The remainder operator gives the part discarded by that whole-number division, so 17 % 5 is 2.
int a = 17;
int b = 5;
System.out.println(a / b); // 3
System.out.println(a % b); // 2A unary arithmetic operator works with one value instead of two. The unary plus expression +a produces the same value as a, and the unary minus expression -a produces the negated value. These signs produce values without changing a. Increment and decrement are different: a++ adds one to a, while a-- subtracts one from a.
int a = 17;
System.out.println(+a); // 17
System.out.println(-a); // -17
a = 17;
a++;
System.out.println(a); // 18
a = 17;
a--;
System.out.println(a); // 16Reset a to 17 before each independent calculation so the results do not depend on an earlier update. After the standalone statement a++, Java stores 18 back into a. After the standalone statement a--, Java stores 16 back into a. The expressions +a and -a only produce values here, so they leave a equal to 17. The difference between prefix and postfix forms inside larger expressions belongs to a separate problem; this standalone use only shows the stored update.
Reset a to 17, then execute a++ as a standalone statement. What value is stored in a afterward?
int a = 17;
a++;Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Set a to 17 and b to 0, then apply the binary operators separately. Addition, subtraction, and multiplication still have ordinary integer results: 17 + 0 is 17, 17 - 0 is 17, and 17 * 0 is 0. Zero becomes a problem when it is the divisor, the value on the right side of / or %.
int a = 17;
int b = 0;
System.out.println(a + b); // 17
System.out.println(a - b); // 17
System.out.println(a * b); // 0
// Each of these statements throws ArithmeticException:
// System.out.println(a / b);
// System.out.println(a % b);There are no complete groups when the divisor is zero, so Java cannot produce an integer quotient or remainder. Evaluating 17 / 0 throws ArithmeticException, and evaluating 17 % 0 throws ArithmeticException as well. The exception happens while the statement runs, before either expression can print a result.