Java Expressions and Operators
When several operators appear in one expression without parentheses, precedence decides which operator is evaluated first, using the ranking from highest to lowest.
Try it: Order of Operations
How Python decides which operator in an expression runs first — precedence, left-to-right order and right-to-left **, plus what /, // and % really return.
How it works
- Parentheses first; then **, which groups right to left and binds tighter than a minus sign on its left.
- Then *, /, // and %, left to right; then + and −, left to right.
- / always gives a float; // rounds down; % takes the sign of the divisor.
- Each step replaces one operation with its value until one number is left.
Default run (7 steps): Expression: 2 + 3 * (7 - 4) ** 2 // 5. Python evaluates left to right, but higher-precedence operators bind first. … Result: 7 (an int).
Simplified: Numbers (at most 6 digits) and arithmetic operators only; parsed and evaluated by the lab's own evaluator (no eval), checked against real Python results.
Loading the simulation…
A Line of Code That Can Mislead
Real Java code rarely contains a single operator. A single line often combines arithmetic, comparisons, logical tests, and assignment, and each of those operators must be applied in some sequence. Before reading any further, try to predict the result of the following expression, working only from what you already know. Do not run anything yet.
What do you think happens?
What value does 2 + 3 * 4 - 1 produce in Java?
Reveal answer
13
Java evaluates 3 * 4 first, because multiplication has higher precedence than addition and subtraction. That gives 2 + 12, which is 14. Then 14 - 1 gives 13. If your prediction was 14 or 15, your mental model applied the operators in a different order than the language does.
If your prediction differed from 13, you have found the exact spot where your mental model and the language's rules disagree. The rest of this article explains the rules that decide the order, and shows how to read an expression without guessing.
Tracing the Order of 2 + 3 * 4 - 1
Precedence is the rule that decides which operator is evaluated first when several operators appear in one expression without parentheses. The source describes precedence as a language-defined ranking, from highest to lowest, where higher-ranked operators are evaluated first. Java does not decide the order by reading position alone. The diagram below shows the nesting that Java applies to the expression from the opening question.
Read the tree from the bottom up. The multiplication 3 * 4 sits deepest, so it is completed first and produces 12. That result feeds the addition, giving 2 + 12, which is 14. The subtraction is the outermost operation, so it runs last and gives 14 - 1, which is 13. The tree shows nesting, but the same sequence can be written as three short steps.
Evaluating 2 + 3 * 4 - 1 by Precedence
Determine the value of 2 + 3 * 4 - 1 by applying the precedence ranking.
- 1Identify the operators
The expression contains a multiplication, an addition, and a subtraction. Multiplication ranks above addition and subtraction, so it is evaluated first.
- 2Evaluate the multiplication
3 * 4 gives 12. The expression is now 2 + 12 - 1.
- 3Evaluate the addition and subtraction
Addition and subtraction share a precedence level, so the grouping follows the associativity rule for left-to-right operators. 2 + 12 gives 14, and then 14 - 1 gives 13.
The value is 13.
13- The expression has no parentheses, so precedence alone decides the order of the operations.
- Multiplication runs first, so the program prints 13 rather than 14 or 15.
The Precedence Ranking and What It Settles
Operator precedence is a language-defined ranking that determines which operator is applied first in an expression that contains several operators and no parentheses. It is a fixed ranking, not the order in which operators happen to appear on the line.
The source presents the ranking as a table listing operators from highest precedence, which are evaluated first, to lowest precedence, which are evaluated last. Each row is a group of operators that share one precedence level, and higher rows bind more tightly than lower rows. Read the table from the top down. The source notes that the assignment operators sit at the bottom, which fits the logic of the ranking: the operators that compute values come before the operators that store them. The ranking covers the operators this section discusses, so treat it as the reference for those operators rather than a list of every Java operator.
Precedence answers one question: when the levels of two operators differ, which one binds more tightly? It does not decide anything when the operators share a level. That is the job of associativity, which is covered next.
Grouping Chains of Equal-Precedence Operators
Operators written on the same row of the ranking have the same precedence. When operators of one level are strung together without parentheses, precedence gives no preference between them. The second rule, associativity, decides the grouping. The source states it this way: unary operators and assignment operators are evaluated right-to-left, and the remaining operators are evaluated left-to-right.
Keep the two questions separate. Precedence asks which operator binds more tightly when the levels differ. Associativity asks how to group a chain when the levels are the same. Learners often make errors by answering the first question when the second was being asked.
The source gives the example A * B / C, which means (A * B) / C. Multiplication and division share a level, and neither is a unary or assignment operator, so the chain is grouped left-to-right. The multiplication is completed first, and its result is then divided by C. The diagram below applies the same left-to-right rule to a subtraction chain, where the grouping changes the answer.
Java groups 100 - 20 - 5 left-to-right, as (100 - 20) - 5. The first subtraction gives 80, and then 80 - 5 gives 75. If the operators were grouped right-to-left, the expression would instead be 100 - (20 - 5), which gives 85. The default grouping decides the answer, so a learner who assumes the wrong grouping gets a different result without any syntax error.
Assignment is the other case the source names, and it is grouped right-to-left. A = B = C is read as A = (B = C). The inner assignment runs first, and the value it produces is then assigned to A. The source asks a useful question here: because the value of B = C as an expression equals the value assigned to B, the form A = B = C can store one value in two variables at once. Think about what happens to a single value that must be stored in two places.
Parentheses as an Explicit Evaluation Order
Parentheses let you replace the default grouping with the grouping you actually want. The operations inside parentheses are settled before the operators outside them. The source gives direct advice on this: if you do not use parentheses to indicate the order of evaluation, you must rely on the precedence rules, and to avoid confusing yourself or a reader, you should use parentheses liberally.
The comparison below shows what happens when the same numbers are combined with and without parentheses. The first column follows the default precedence rules. The second column forces the grouping the author might have intended, and the results diverge at the first step.
The two columns use the same numbers and the same operators, but the groupings differ. The unparenthesized line is evaluated as 2 + (3 * 4) - 1, giving 13. The parenthesized line forces the additions and subtractions to be settled first, giving 15. The divergence occurs at the first step: the unparenthesized version multiplies 3 by 4, while the parenthesized version adds 2 and 3. Locating that first differing step is the most reliable way to find where a surprising result came from.
Mistakes Learners Make When Reading Expressions
Common mistake Assuming Java always reads an expression from left to right
What it looks likeIn 2 + 3 * 4 - 1, reading left to right gives (2 + 3) = 5, then 5 * 4 = 20, then 20 - 1 = 19.
Why it failsReading position does not decide the order. Precedence does, and multiplication ranks above addition and subtraction.
Do this insteadFind the operators with the highest precedence first, then apply the rest in the order the ranking dictates.
Common mistake Applying school arithmetic habits to every operator
What it looks likeAssuming that all Java operators follow the same rule as arithmetic multiplication and addition.
Why it failsPrecedence is a specific ranking defined by the language, and it covers more than arithmetic operators.
Do this insteadConsult the precedence ranking for the operators you are using.
Common mistake Answering the precedence question when the grouping question was asked
What it looks likeFor 100 - 20 - 5, saying the subtractions differ in precedence and therefore cannot be grouped.
Why it failsBoth subtractions share a level, so precedence gives no preference. Associativity decides the grouping.
Do this insteadApply the left-to-right associativity rule to get (100 - 20) - 5, which equals 75.
Common mistake Expecting assignment chains to group left-to-right
What it looks likeReading A = B = C as (A = B) = C.Why it failsAssignment operators are grouped right-to-left, so the source reads A = B = C as A = (B = C).
Do this insteadEvaluate the rightmost assignment first, then assign its result to the next variable to the left.
Common mistake Leaving a long expression without parentheses when the intended order is not obvious
Practice Tracing Expressions
Without running the code, determine the value of 100 - 20 - 5 and the value of 100 - (20 - 5). Then state which of the two expressions Java evaluates by default, and explain which operator grouping the default follows.
Hints
- Both subtractions in the first expression share one precedence level, so precedence gives no preference between them.
- Subtraction is a left-to-right operator under the associativity rule, so the first subtraction is completed first.
- Parentheses in the second expression force the inner subtraction to be settled first.
Predict the value of 10 + 2 * 3 - 4 / 2, then rewrite the expression with parentheses so that the addition is evaluated first. Identify the first step at which the two versions differ.
Hints
- Multiplication and division rank above addition and subtraction, so 2 * 3 and 4 / 2 are settled before the outer operators.
- Divide the expression into the operations that precedence groups first, then trace what the parenthesized version does differently.
Key Takeaways
- When several operators appear in one expression without parentheses, precedence decides which operator is evaluated first, using a language-defined ranking from highest to lowest.
- Precedence is not the same as reading position or the ordinary order of operations from arithmetic, so read each operator against the ranking rather than against its location.
- Operators on the same precedence level are grouped by associativity: unary and assignment operators right-to-left, and the remaining operators left-to-right.
- Parentheses state the evaluation order explicitly and are the recommended way to make an expression clear to yourself and to readers.
- When a result is surprising, locate the first step where your grouping differs from the language's grouping, because that is where the mental model and the rules disagree.