AP® Computer Science A review sheet from Aim for Five (aimforfive.com/csa/units/1/1-3)
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
intvalues give anint. For/, that means Java keeps only the whole-number part and throws the rest away:7 / 2is3and2 / 5is0. - If at least one operand is a
double, the result is adouble:7.0 / 2is3.5. %gives the remainder after division:17 % 5is2,6 % 3is0, and5 % 7is5(7 goes into 5 zero times, with 5 left over).- Dividing an
intby theint0, with/or%, throws anArithmeticExceptionwhile 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.
| Expression | How Java groups it | Value |
|---|---|---|
| 2 + 3 * 4 | 2 + (3 * 4) | 14 |
| (2 + 3) * 4 | Parentheses first | 20 |
| 10 - 4 - 3 | (10 - 4) - 3 | 3 |
| 8 / 2 * 4 | (8 / 2) * 4 | 16 |
| 12 % 5 * 2 | (12 % 5) * 2 | 4 |
| 1 + 9 / 2 * 2.0 | 1 + ((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.
- 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 solutionHide the solution
- Step 1: Line 1:
printdisplaysAand stays on the same line. - Step 2: Line 2: division happens before
+, so7 / 2is worked out first. Both areints, so it's3. Then"B" + 3joins to make"B3".printlndisplaysB3right after theA, then moves to a new line. So far:AB3. - Step 3: Line 3:
7 % 3is1(7 divided by 3 is 2 remainder 1), plus 1 is2.printdisplays2and stays on the line. - 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, then2 "done". - Step 1: Line 1:
- 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 solutionHide the solution
- Step 1:
a + b + cis85 + 90 + 93, which is theint268. - Step 2: For
avg1,268 / 3divides twoints, so Java keeps only the whole part: 89. Storing 89 in adoublemakes it89.0. The decimal part was lost before the value was ever stored. - Step 3: For
avg2,268 / 3.0has adoubleoperand, so the division is exact: about 89.333. Java prints adoublewith as many digits as it needs, which here is89.33333333333333.
Answer: It prints
89.0and then89.33333333333333. Onlyavg2is the true average; dividing by3.0(or casting, as in 1.5) fixes the logic error inavg1. - Step 1:
Common mistakes
- Expecting
7 / 2to be3.5. With twoints, the answer is always anint, here3. - Storing an
intdivision in adoubleand thinking that fixes it.double d = 7 / 2;gives3.0, because the division happens first. - Mixing up
printandprintlnwhen 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 == 0to test for even numbers and adoubleoperand (or a cast) when you need an exact average.
Connected topics
Videos
Check yourself
4 questions on 1.3 Expressions and Output. Pick an answer to see if you got it, and why.
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?
Consider the following code segment.System.out.println(17 / 5 + 17 % 5);What is printed as a result of executing the code segment?
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?
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