The Stacks
UNIT 1: USING OBJECTS AND METHODS · TOPIC 1.3

1.3 Expressions and Output

Arithmetic in Java has one rule that catches everyone: dividing two ints gives an int. Everything else follows normal math.

What you need to know

  • Operators: +, -, *, /, %. Normal precedence: parentheses, then * / % left to right, then + - left to right.
  • Integer division: when both operands are int, / truncates (drops the decimal). 7 / 2 is 3, -7 / 2 is -3. If either operand is a double, the result is a double: 7 / 2.0 is 3.5.
  • % is the remainder: 7 % 2 is 1, 10 % 5 is 0. For negative dividends the result is negative: -7 % 2 is -1.
  • Dividing an int by zero throws an ArithmeticException. Dividing a double by zero gives Infinity or NaN, no exception.
  • System.out.println(x) prints x and moves to a new line; System.out.print(x) prints without a newline.
  • + with a String is concatenation. Evaluation is left to right: "Sum: " + 1 + 2 is "Sum: 12", but "Sum: " + (1 + 2) is "Sum: 3".
  • String literals are in double quotes; escape sequences include \n (newline), \" (quote), \\ (backslash).

Worked example

System.out.println(17 / 5);        // 3
System.out.println(17 % 5);        // 2
System.out.println(17 / 5.0);      // 3.4
System.out.println(17.0 / 5);      // 3.4
System.out.println(1 + 2 + "3");   // 33
System.out.println("1" + 2 + 3);   // 123
System.out.println(2 * 3 % 4);     // 2   (6 % 4)

Lines 5 and 6 are the concatenation trap: left-to-right evaluation means the ints are added before the String is reached in line 5, but not in line 6.

Exam tip: Look at both operands of every /. Both int? Truncate. Either double? Decimal result. For the + trap, scan left to right and note the moment a String appears — from then on everything is concatenation.

Going deeper

The nuance, edge cases, and connections that turn a 3 into a 5.

  • Integer division truncates toward zero. 7 / 2 is 3; -7 / 2 is −3 (not −4). Truncation, not floor. The exam tests negatives specifically.
  • Remainder sign follows the dividend (left operand): -7 % 3 is −1; 7 % -3 is 1. The identity that always holds: a == (a / b) * b + a % b.
  • Mixed arithmetic promotes to double: if either operand is a double, the whole operation is done in double. 1 / 2 * 2.0 is 0.0 (integer division first: 0, then 0 * 2.0). 1 / 2.0 * 2 is 1.0. Order matters because promotion happens per operation, not per expression.
  • String concatenation is left-to-right. Once a String is on the left of a +, everything after is concatenated. 1 + 2 + "3" + 4 + 5 is "3345": 1 + 2 = 3, then "3" + "3" = "33", then + 4 → "334", + 5 → "3345".
  • System.out.println() with no argument prints a blank line. print vs println matters for exact-output questions: with print, consecutive outputs run together on one line.
  • Printing a double prints the full value: System.out.println(10.0 / 4) shows 2.5; 10.0 / 5 shows 2.0, not 2. The .0 is part of the answer.
  • Escape sequences: \n newline, \t tab, \" literal quote, \\ literal backslash. The exam uses \n and \" most.

Mistakes that cost points

  • Rounding integer division. 7 / 2 is 3, never 3.5 or 4. Two ints in, int out, fraction dropped.
  • Flooring negative division. -7 / 2 is −3. Java truncates toward zero.
  • Adding before concatenating. "Total: " + 5 + 10 is "Total: 510". The String came first, so no addition happens.
  • Dropping the .0 on doubles. If the result is a double, the output shows a decimal point. 4.0, not 4.

Practice questions

Written in the style of the real exam. Try each one before revealing the answer.

Q1 What is printed by System.out.println(25 / 4 * 2.0);?
  1. A 12.5
  2. B 12.0
  3. C 12
  4. D 13.0
Show answer

Answer: B. 25 / 4 is integer division = 6. Then 6 * 2.0 = 12.0 (double).

Q2 What is printed by System.out.println("Total: " + 5 + 10);?
  1. A Total: 15
  2. B Total: 510
  3. C A compile-time error
  4. D 15
Show answer

Answer: B. Left to right: "Total: " + 5 → "Total: 5", then + 10 → "Total: 510".

Q3 What is the value of -13 % 5?
  1. A 3
  2. B -3
  3. C 2
  4. D -2
Show answer

Answer: B. Java's remainder keeps the sign of the dividend: -13 = 5 × (-2) + (-3), so the result is -3.

Key vocabulary

Integer division
int / int drops the fractional part
Remainder (%)
the remainder after division; sign follows the left operand
Concatenation
joining Strings with +; any + involving a String produces a String
println
prints a value followed by a newline