1.3 Expressions and outputJava does math its own way

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?
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
- line 3 int / int: the .4 is thrown away
- line 4 The remainder of 17 divided by 5
- line 5 One double operand keeps the decimals
- line 6 Truncates toward zero; the remainder keeps the sign of 17
- line 7 1 + 2 is 3, then the String takes over
- line 8 Parentheses make the addition happen first
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?
| Step | Do this | Expression becomes |
|---|---|---|
| start | 3 + 17 % 5 * 2 - 7 / 2 | |
| 1 | 17 % 5 is 2 | 3 + 2 * 2 - 7 / 2 |
| 2 | 2 * 2 is 4 | 3 + 4 - 7 / 2 |
| 3 | 7 / 2 is 3 | 3 + 4 - 3 |
| 4 | 3 + 4 is 7 | 7 - 3 |
| 5 | 7 - 3 is 4 | 4 |
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");
- line 1 \" puts a quote mark inside the String
- line 2 \\ prints one backslash
- line 3 \n starts a new line in the middle
- line 4 An empty println just ends the line
I thought about it. Show me
What is printed?
int r = 25 - 4 * 3 % 5 + 9 / 2;
System.out.println(r);
System.out.println("r = " + r / 2 + 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. - Now the + and -, left to right: 25 - 2 is 23, then 23 + 4 is 27. r is 27, and the first println prints 27.
- 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. - 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
- Do every *, / and % first, left to right. Remember that 9 / 2 is 4.
- For the last line, r / 2 happens before either +. What's 27 / 2 with ints?
- After "r = " + 13, the left side is a String. What does + 1 do to a String?
System.out.println(10 % 4 + 10 / 4 * 1.5);
- A5.0
- B5
- C5.75
- 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.
Which statement prints exactly this line?
She said "go"
- ASystem.out.println("She said "go"");
- BSystem.out.println("She said \"go\"");
- CSystem.out.println("She said \\go\\");
- 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.

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.