Scrivio Interview library
CodingFoundations5 min read · updated 12 November 2026

What do these print?

Java
int x = 5;
System.out.println(x++ + ++x);
System.out.println(1 + 2 + "3" + 4 + 5);
System.out.println(10 - 2 * 3 % 4);

The first line prints 12: x++ yields 5 and then makes x 6, and ++x makes x 7 and yields 7, so the sum is 5 + 7. The second prints 3345, because + is evaluated left to right: 1 + 2 is the int 3, then 3 + "3" is the string "33", and every later + appends, giving "334" and "3345". The third prints 8, because *, / and % have the same precedence and associate left to right, so 2 * 3 is 6, 6 % 4 is 2, and 10 - 2 is 8.

Why this question is asked

Expression puzzles like these show up in interviews because they test three separate rules at once: what increment operators return, how precedence and associativity group operators, and in what order Java evaluates operands. Real code rarely looks like x++ + ++x, but the same rules decide what logging statements print, how string building behaves with numbers, and why some arithmetic gives unexpected results. In this answer, we will recap precedence, associativity and evaluation order, then work through each line step by step, and finally see why Java guarantees the answers where C does not.

1A Quick Recap of Precedence, Associativity and Evaluation Order

Precedence decides which operators bind more tightly when different operators meet: in 10 - 2 * 3, the * binds first, so it means 10 - (2 * 3). Associativity decides how operators of the same precedence group: most binary operators in Java associate to the left, so a - b - c means (a - b) - c. Evaluation order decides when each operand is actually computed, and Java's rule is simple: the left operand is fully evaluated before the right one, and all operands are evaluated before the operation is performed.

These are different rules. Precedence and associativity only decide the shape of the expression, which operator applies to which operands. Evaluation order decides the sequence in which side effects, such as x++, happen. Puzzles are hard when we mix them up.

Operators used in the puzzles, highest precedence first
OperatorsKindAssociativity
x++ x--postfixn/a
++x --x unary + -prefix and unaryright to left
* / %multiplicativeleft to right
+ -additive and string concatenationleft to right
= += -=assignmentright to left
Equal precedence plus left associativity is what drives lines two and three.

2Line 1: x++ + ++x

Postfix x++ returns the current value of x and then increments it. Prefix ++x increments first and returns the new value. Java evaluates the left operand of + completely, including its side effect, before it starts the right operand.

Java
int x = 5;
int left = x++; // left = 5, x is now 6
int right = ++x; // x is now 7, right = 7
System.out.println(left + right); // 12
int y = 5;
System.out.println(y++ + ++y); // 12: exactly the same steps
System.out.println(y); // 7

Notice how the parser reads the characters x++ + ++x. Java's tokenizer takes the longest operator it can at each point, so it sees x, ++, +, ++, x. Writing x+++++x without spaces would read as x ++ ++ + x and fail to compile, which is a nice reminder that spaces are not decoration in these puzzles.

3Line 2: Mixing Numbers and Strings

The + operator means addition when both operands are numbers and string concatenation when either operand is a String. Because + associates to the left, the expression is grouped as ((((1 + 2) + "3") + 4) + 5), and each step decides its meaning from the types it sees at that moment.

Java
System.out.println(1 + 2 + "3" + 4 + 5); // 3345
// 1 + 2 -> 3 (int + int: addition)
// 3 + "3" -> "33" (int + String: concatenation)
// "33" + 4 -> "334"
// "334" + 5 -> "3345"
System.out.println("3" + 4 + 5); // 345
System.out.println("3" + (4 + 5)); // 39: parentheses force addition first
System.out.println(1 + 2 + ""); // 3
Step throughEvaluating 1 + 2 + "3" + 4 + 5
  1. 1 + 2int + int = 3
  2. 3 + "3"a String is involved: "33"
  3. "33" + 4"334"
  4. "334" + 5"3345"

4 steps in order. Play them, or step through them one at a time.

Each + looks only at its own two operands; once the left side is a String, everything is appended.

So numbers before the first string are added, and everything after it is appended. This is a real source of bugs in log and message building, such as "Total: " + price + tax printing two numbers side by side instead of their sum. Parentheses, or String.format, make the intent explicit.

4Line 3: Equal Precedence and Left Associativity

In 10 - 2 * 3 % 4, the * and % bind more tightly than -, so the right side of the minus is 2 * 3 % 4. Since * and % have the same precedence and associate to the left, that is (2 * 3) % 4.

Java
System.out.println(10 - 2 * 3 % 4); // 8
// 2 * 3 -> 6
// 6 % 4 -> 2
// 10 - 2 -> 8
System.out.println(10 - 2 * (3 % 4)); // 4: 3 % 4 is 3, 2 * 3 is 6, 10 - 6 is 4

A common wrong answer is 4, which comes from assuming % binds more tightly than *. It does not; they are equal, and the leftmost one goes first. When an expression depends on that kind of detail, parentheses make it readable to the next person.

5Why Java Guarantees These Answers

The Java Language Specification fixes the order: operands are evaluated left to right, and every operand is evaluated before the operator is applied. So x++ + ++x prints 12 on every JVM and with every compiler. In C and C++, modifying the same variable twice without a sequence point in between, as in i++ + ++i, is undefined behaviour: the compiler may produce any result, and different compilers really do. Java chose predictability, which makes such code legal and deterministic. It does not make it readable, and in real code we would write the increments as separate statements.

6Follow-up questions

Why is x = x++; a no-op for x?

Because of the evaluation order. The right side x++ is evaluated first: it saves the current value, say 5, and increments x to 6. Then the assignment stores the saved value, 5, back into x, overwriting the increment. So x ends as 5. Static analysis tools flag this pattern because it is never what the author meant; the intent was either x++ or x = x + 1.

Does the JIT really evaluate operands left to right?

The JIT may reorder machine instructions, but only in ways that produce the same observable result as left-to-right evaluation within a thread. So for our purposes the language rule is what we see.

What does 'a' + 'b' + "c" print?

195c. The first + adds two chars, which are promoted to int: 97 + 98 is 195. Then 195 + "c" concatenates. To get abc, start with a string: "" + 'a' + 'b' + "c".

7Conclusion

In this answer, we used three rules to work out each line. x++ returns the old value and ++x the new one, and Java evaluates the left operand of + first, so x++ + ++x is 5 + 7 = 12. Left-associative + switches from addition to concatenation as soon as one side is a String, so 1 + 2 + "3" + 4 + 5 is "3345". And *, / and % share one precedence level and group left to right, so 10 - 2 * 3 % 4 is 8. Java fixes evaluation order to left to right, so these results are guaranteed, unlike in C.

What to say in the interviewAbout 90 seconds aloud

Open with

The first line prints 12. Java evaluates operands left to right, so x++ runs first: it yields the current value 5 and then increments x to 6. Then ++x increments x to 7 and yields 7, so the sum is 5 plus 7.

Then, if they want more

The second line prints 3345. The plus operator associates to the left, and each plus decides whether it is addition or concatenation from its own two operands. 1 plus 2 is the int 3; 3 plus the string 3 becomes the string 33, and from then on everything is appended, so 334 and then 3345.

If I wanted the 4 and 5 added, I would need parentheses. The third line prints 8. Multiplication and modulo have higher precedence than subtraction, and they have the same precedence as each other, so they associate left to right: 2 times 3 is 6, 6 mod 4 is 2, and 10 minus 2 is 8.

A common mistake is to think modulo binds tighter and get 4. The reason all of this is predictable is that the Java Language Specification guarantees left-to-right evaluation of operands, while in C something like i++ plus ++i is undefined behaviour.

The related follow-up, x equals x++, leaves x unchanged, because the old value is saved, x is incremented, and then the saved value is assigned back.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What do these print?": https://scrivio-interview-library.netlify.app/java/operators-and-control-flow/fc57ea1359/