Skip to main content

Unit 1 · Topic 1.3

1.3 Expressions and Output

This topic is about producing output and working out arithmetic exactly the way Java does. Integer division, the % operator and operator precedence show up in a huge number of exam questions, so it pays to get these rules automatic.

Key terms

  • string literal
  • escape sequence
  • integer division
  • remainder operator %
  • operator precedence
  • ArithmeticException

print and println

System.out.println(x) displays x and then moves to a new line. System.out.print(x) displays x and stays on the same line, so the next output continues right after it.

System.out.print("Ready"); System.out.println(" set"); System.out.println("Go!");

This prints Ready set on the first line and Go! on the second. The space before set is part of the string, which is why the words don't run together.

Literals and escape sequences

A literal is a fixed value typed straight into the code, like 42, 3.5, true or "hello". A string literal is any sequence of characters in double quotes.

Some characters can't be typed directly inside a string, so Java uses escape sequences, which start with a backslash: \" is a double quote, \\ is one backslash, and \n is a new line.

System.out.println("She said \"hi\""); System.out.println("C:\\games"); System.out.println("line 1\nline 2");

The output is three statements but four lines: She said "hi", then C:\games, then line 1 and line 2 on separate lines, because \n starts a new line in the middle of the third string.

Arithmetic and integer division

The arithmetic operators are +, -, *, / and %. The type of the result depends on the operands (the values being combined):

  • Two int values give an int. For /, that means Java keeps only the whole-number part and throws the rest away: 7 / 2 is 3 and 2 / 5 is 0.
  • If at least one operand is a double, the result is a double: 7.0 / 2 is 3.5.
  • % gives the remainder after division: 17 % 5 is 2, 6 % 3 is 0, and 5 % 7 is 5 (7 goes into 5 zero times, with 5 left over).
  • Dividing an int by the int 0, with / or %, throws an ArithmeticException while the program runs.

Operator precedence

Java groups operators by precedence. *, / and % come before + and -. Operators at the same level go left to right. Parentheses override both rules.

ExpressionHow Java groups itValue
2 + 3 * 42 + (3 * 4)14
(2 + 3) * 4Parentheses first20
10 - 4 - 3(10 - 4) - 33
8 / 2 * 4(8 / 2) * 416
12 % 5 * 2(12 % 5) * 24
1 + 9 / 2 * 2.01 + ((9 / 2) * 2.0)9.0

Not on the exam

You won't see % with a negative left side or a right side of 0 or less, and you won't be asked what happens when you divide a double by zero. Expressions are always grouped by precedence before any type rules apply, so in the last row, 9 / 2 is integer division (4) even though a double appears later.

Worked examples

Try each one yourself first, then open the solution.

  1. Example 1

    Tracing mixed output

    What is printed when this code runs?System.out.print("A"); System.out.println("B" + 7 / 2); System.out.print(7 % 3 + 1); System.out.println(" \"done\"");

    Show the solution
    1. Step 1: Line 1: print displays A and stays on the same line.
    2. Step 2: Line 2: division happens before +, so 7 / 2 is worked out first. Both are ints, so it's 3. Then "B" + 3 joins to make "B3". println displays B3 right after the A, then moves to a new line. So far: AB3.
    3. Step 3: Line 3: 7 % 3 is 1 (7 divided by 3 is 2 remainder 1), plus 1 is 2. print displays 2 and stays on the line.
    4. Step 4: Line 4: the string is a space, then an escaped quote, the word done, and another escaped quote. It prints "done" and ends the line.

    Answer: Two lines: AB3, then 2 "done".

  2. Example 2

    The integer-division trap

    A student wants the exact average of three test scores. What does this code print?int a = 85; int b = 90; int c = 93; double avg1 = (a + b + c) / 3; double avg2 = (a + b + c) / 3.0; System.out.println(avg1); System.out.println(avg2);

    Show the solution
    1. Step 1: a + b + c is 85 + 90 + 93, which is the int 268.
    2. Step 2: For avg1, 268 / 3 divides two ints, so Java keeps only the whole part: 89. Storing 89 in a double makes it 89.0. The decimal part was lost before the value was ever stored.
    3. Step 3: For avg2, 268 / 3.0 has a double operand, so the division is exact: about 89.333. Java prints a double with as many digits as it needs, which here is 89.33333333333333.

    Answer: It prints 89.0 and then 89.33333333333333. Only avg2 is the true average; dividing by 3.0 (or casting, as in 1.5) fixes the logic error in avg1.

Common mistakes

  • Expecting 7 / 2 to be 3.5. With two ints, the answer is always an int, here 3.
  • Storing an int division in a double and thinking that fixes it. double d = 7 / 2; gives 3.0, because the division happens first.
  • Mixing up print and println when tracing output. Track where the cursor is after each statement.
  • Doing + before * or %. Check precedence, and remember that % is at the same level as * and /.

On the exam

  • "What is printed?" questions are common. Write the output character by character, including spaces and line breaks, and evaluate every expression with Java's rules, not calculator rules.
  • In free-response code, use % 2 == 0 to test for even numbers and a double operand (or a cast) when you need an exact average.

Connected topics

Videos

  • AP Computer Science A - Topic 1.3 - Part 1: Expressions and Output

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • int and double Division (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

  • AP Java - Basic Math

    CodeHSWatch on YouTube (opens in a new tab)

  • AP Computer Science A - Topic 1.3 - Part 2: Expressions and Output

    Tim Gallagher Computer ScienceWatch on YouTube (opens in a new tab)

  • Java arithmetic is easy! 🧮

    Bro CodeWatch on YouTube (opens in a new tab)

  • Operator Precedence / Order Of Operation (Java Tutorial)

    Bill BarnumWatch on YouTube (opens in a new tab)

Check yourself

4 questions on 1.3 Expressions and Output. Pick an answer to see if you got it, and why.

Question 1 of 4

Consider the following code segment.System.out.print("Go"); System.out.print("Team"); System.out.println("!");What is printed as a result of executing the code segment?

Question 2 of 4

Consider the following code segment.System.out.println(17 / 5 + 17 % 5);What is printed as a result of executing the code segment?

Question 3 of 4

Consider the following code segment.double avg = (7 + 8) / 2; System.out.println(avg);What is printed as a result of executing the code segment?

Question 4 of 4

Consider the following code segment.System.out.println("Sum: " + 3 + 4); System.out.println(3 + 4 + " total");What is printed as a result of executing the code segment?

0 of 4 answered