AP Computer Science A · Unit 1 lecture · Part 3 of 11 · 10 min

1.3 Expressions and outputJava does math its own way

Mr. Thompkins
Here's the most-tested idea in the whole unit, and it fits in four characters: 7 / 2. Ask a calculator and you get 3.5. Ask Java and you get 3. By the end of this section you'll know exactly why, and you'll never lose a point to it.

An expression is anything that produces a value: a literal, a variable, or a combination of them with operators. Java's arithmetic operators are + (add), - (subtract), * (multiply), / (divide) and % (remainder).

The type of the result depends on the types going in. If both operands are int, the result is an int. If at least one is a double, the result is a double. That rule is harmless for +, - and *, but for / it changes everything. When both sides of / are int, Java does integer division: it computes the quotient and throws the decimal part away. 17 / 5 is 3, not 3.4. It doesn't round, it truncates, which means it chops toward zero. So -17 / 5 is -3.

?Wait, what's a remainder again?
When you divide 17 by 5, 5 goes in 3 whole times (that's 15), with 2 left over. The 3 is the quotient and the 2 is the remainder. Java's 17 / 5 gives the quotient, 3, and 17 % 5 gives the remainder, 2. You can always check: 5 × 3 + 2 = 17.

The % operator gives the remainder. It's more useful than it looks: n % 2 is 0 exactly when n is even, n % 10 is the last digit of a positive number, and n % 7 tells you where you land in a 7-day cycle. When the left side is negative, the remainder is negative or zero: -17 % 5 is -2. Dividing an int by 0, or taking % 0, throws an ArithmeticException.

1int a = 17;2int b = 5;3System.out.println(a / b);14System.out.println(a % b);25System.out.println(a / 2.0);36System.out.println(-17 / 5 + " " + -17 % 5);47System.out.println(1 + 2 + "3" + 4 + 5);58System.out.println("Total: " + (a + b));6
  1. line 3 int / int: the .4 is thrown away
  2. line 4 The remainder of 17 divided by 5
  3. line 5 One double operand keeps the decimals
  4. line 6 Truncates toward zero; the remainder keeps the sign of 17
  5. line 7 1 + 2 is 3, then the String takes over
  6. line 8 Parentheses make the addition happen first
output › 3 2 8.5 -3 -2 3345 Total: 22
Integer division, remainder and String concatenation, run under Java 17. Line 7 surprises almost everyone the first time.

Order of operations

Java uses the precedence you know from math. Parentheses first. Then *, / and %, which are all on the same level. Then + and -. Operators on the same level go left to right. So in 3 + 17 % 5 * 2 - 7 / 2, the %, * and / happen first, left to right, and then the + and -.

?Wait, what's order of operations again?
It's the agreement about which part of an expression you work out first. In math class it's often remembered as PEMDAS: parentheses, exponents, multiplication and division (left to right), then addition and subtraction (left to right). Java has no exponent operator (it uses Math.pow, topic 1.11), and it puts % on the same level as * and /.
StepDo thisExpression becomes
start3 + 17 % 5 * 2 - 7 / 2
117 % 5 is 23 + 2 * 2 - 7 / 2
22 * 2 is 43 + 4 - 7 / 2
37 / 2 is 33 + 4 - 3
43 + 4 is 77 - 3
57 - 3 is 44
Evaluating one operator at a time. On the exam, write the expression again after each step. It is slow, and it is how you stop making mistakes.

Printing, and what + does to Strings

System.out.print(x) prints x and stays on the same line. System.out.println(x) prints x and then moves to a new line. A String literal is text inside double quotes, like "Total: ". When + has a String on either side, it stops adding and starts concatenating, which means gluing the text together. Any number on the other side is turned into text first.

Because + goes left to right, where the String appears matters. In 1 + 2 + "3" + 4 + 5, Java first does 1 + 2, which is the number 3. Then 3 + "3" is the String "33". From then on, every + glues, so the result is "3345". If you want math inside a printed sentence, wrap it in parentheses: "Total: " + (a + b).

To put special characters inside a String, use an escape sequence, a backslash followed by a character. \" is a quote mark, \\ is a single backslash, and \n is a new line.

1System.out.println("Mr. T said \"trace it\"");12System.out.println("Folder: C:\\notes");23System.out.print("one\ntwo");34System.out.println();45System.out.println("done");
  1. line 1 \" puts a quote mark inside the String
  2. line 2 \\ prints one backslash
  3. line 3 \n starts a new line in the middle
  4. line 4 An empty println just ends the line
output › Mr. T said "trace it" Folder: C:\notes one two done
The three escape sequences you need. In the output, \n became a real line break.
Pause and think
Predict the output: System.out.println(7 + 3 + " apples " + 7 + 3);
I thought about it. Show me
10 apples 73. Left to right: 7 + 3 is the number 10. Then 10 + " apples " is the String "10 apples ". After that, every + glues text, so 7 and then 3 are attached as characters.
Evaluate it the way Java does

What is printed?

int r = 25 - 4 * 3 % 5 + 9 / 2;
System.out.println(r);
System.out.println("r = " + r / 2 + 1);
  1. First line, highest level first, left to right: 4 * 3 is 12. Then 12 % 5 is 2. Then 9 / 2 is 4, because both are ints.
    Why?
    *, / and % are on the same level, so you take them left to right: the * comes before the % here.
  2. Now the + and -, left to right: 25 - 2 is 23, then 23 + 4 is 27. r is 27, and the first println prints 27.
  3. Third line: / is higher than +, so r / 2 happens first. 27 / 2 is 13 (integer division).
    Why?
    Precedence still applies inside a println. The / beats both + signs.
  4. Then left to right: "r = " + 13 is the String "r = 13". Then "r = 13" + 1 glues on the character 1, giving "r = 131".
    Why?
    Once the left side is a String, + concatenates. To print 14 you'd need parentheses: "r = " + (r / 2 + 1).

Answer: 27, then r = 131.

Stuck? Get a hint
  1. Do every *, / and % first, left to right. Remember that 9 / 2 is 4.
  2. For the last line, r / 2 happens before either +. What's 27 / 2 with ints?
  3. After "r = " + 13, the left side is a String. What does + 1 do to a String?
Check yourself
What is printed?
System.out.println(10 % 4 + 10 / 4 * 1.5);
  1. A5.0
  2. B5
  3. C5.75
  4. D6.0
Show the answer

A. 10 % 4 is 2. 10 / 4 is 2 (int division). 2 * 1.5 is 3.0, a double. 2 + 3.0 is 5.0. Choice C is the trap: it does 10 / 4 as 2.5, but both of those operands are ints, so the decimal part is gone before the 1.5 ever shows up.

Check yourself

Which statement prints exactly this line?

She said "go"

  1. ASystem.out.println("She said "go"");
  2. BSystem.out.println("She said \"go\"");
  3. CSystem.out.println("She said \\go\\");
  4. DSystem.out.println('She said "go"');
Show the answer

B. Each \" puts one quote mark inside the String. Choice A doesn't compile, because the second quote mark ends the String early. Choice C prints backslashes, and choice D uses single quotes, which Java does not allow for Strings.

Exam tip from Mr. T

On every expression question, circle each / and ask, "are both sides int?" Then write the expression again after each operator. The exam's wrong answers are built from exactly the shortcuts you skip.

  • int op int gives an int. If either side is a double, the result is a double.
  • int / int truncates toward zero: 17 / 5 is 3, -17 / 5 is -3. % gives the remainder: 17 % 5 is 2.
  • *, / and % come before + and -. Same level: left to right.
  • Once a String joins the expression, + glues text, left to right. Use parentheses for math inside a printed sentence.
  • print stays on the line, println ends it. Escape sequences: \" \\ \n.