If you mix up the order of operations when calculating a tip, you might pay twice as much as you meant to. In Java, getting the sequence of arithmetic, relational, logical, and assignment operators wrong leads to programs that produce wrong results or crash. This chapter teaches you to build expressions — the sentences of Java code — and understand the strict rules Java uses to interpret them, so you can write code that behaves exactly as you intend.
Jump to a section
A simple way to picture Operators, Expressions, and Operator Precedence
A busy kitchen, just before dinner service. The head chef calls out orders, and the line cooks transform piles of ingredients into finished plates.
Every recipe is an expression: a set of ingredients (values) combined using specific actions (operators) in a strict order (precedence). First, you must chop the onion and mince the garlic — those are like evaluating variables or literals. Then you might sauté them in olive oil: the addition (+) of heat and fat. Next, you add stock and tomatoes, and let it simmer — that is like using the assignment operator (=) to store the result in a variable called "sauce". The order of operations is critical. You cannot add the wine before you’ve deglazed the pan, just like in Java you cannot use a relational operator (like greater than, >) on a result before the arithmetic (like addition, +) is finished. If you put the dessert in the oven before mixing the batter, you get a mess. Similarly, if you forget that multiplication in Java happens before addition (precedence), you get a wrong result. Parentheses in a recipe — "fold in the chocolate (melted, then cooled)" — act like brackets in code: they force a particular sequence. The final plated dish is the output of your expression, ready for the customer. A professional kitchen depends on every cook following the same order, every time, just as Java relies on operator precedence to make every program predictable.
In Java, an expression is any valid combination of values (called operands), operators, and sometimes method calls that produces a single value. Think of an expression like a maths equation: 5 + 3 is an expression whose result is 8. The + is the operator, and 5 and 3 are the operands. Operators are symbols that tell Java to perform a specific action, like adding, comparing, or assigning.
There are four main categories of operators you need to know for the 1Z0-811 exam:
Arithmetic operators: These are the ones you already know from school — + (addition), - (subtraction), * (multiplication), / (division), and % (modulus, which gives the remainder of a division). For example, 10 % 3 gives 1, because 3 goes into 10 three times (9) with a remainder of 1. There are also two unary arithmetic operators: ++ (increment) and -- (decrement). Unary means they work on a single operand. x++ adds 1 to the value of x, and x-- subtracts 1. They can be written before the operand (++x, called pre-increment) or after (x++, post-increment), and the timing matters — pre-increment adds 1 first, then uses the value; post-increment uses the value first, then adds 1.
Relational operators: These compare two values and return a boolean (true or false). They are: == (equal to), != (not equal to), < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to). For example, the expression 7 > 3 evaluates to true, while 5 == 8 evaluates to false. Relational operators are the foundation of making decisions in code — they are used inside if statements and loops to control program flow.
Logical operators: These combine or invert boolean values. The most common ones are && (logical AND — returns true only if both sides are true), || (logical OR — returns true if at least one side is true), and ! (logical NOT — flips true to false and vice versa). For instance, if you have boolean isRaining = true and boolean hasUmbrella = false, the expression isRaining && hasUmbrella is false, but isRaining || hasUmbrella is true. The ! operator is unary: !isRaining evaluates to false. Logical operators often use short-circuit evaluation: for &&, if the left side is false, Java does not even bother evaluating the right side because the result is already known to be false. For ||, if the left side is true, the right side is skipped. This is important for efficiency and for avoiding errors when the right-side expression has side effects.
Assignment operators: The basic assignment operator = stores a value into a variable. For example, int x = 10; assigns 10 to x. There are also compound assignment operators that combine an arithmetic operation with assignment: +=, -=, *=, /=, %=. So x += 5 is shorthand for x = x + 5. These are convenient and slightly more efficient, but they work exactly the same as writing out the long form.
Now, the crucial part: operator precedence. This is a set of rules that determines the order in which Java evaluates operators in a complex expression. For example, in the expression 5 + 3 * 2, Java multiplies 3 * 2 first (getting 6), then adds 5, giving 11 — not 16 as you would get if you added first. That is because multiplication has higher precedence than addition. The table of precedence, from highest to lowest, includes (among others): postfix operators (like x++, x--), then unary operators (like ++x, --x, !), then multiplicative (*, /, %), then additive (+, -), then relational (<, >, <=, >=), then equality (==, !=), then logical AND (&&), then logical OR (||), then assignment (=, +=, etc.). You can always override precedence by using parentheses ( and ), just like in maths — expressions inside parentheses are evaluated first. For clarity, it is often better to use parentheses even when precedence would give you the correct result, because it makes your code easier for other programmers (and your future self) to read.
Expressions are everywhere in Java: in variable declarations (int sum = a + b;), in if conditions (if (score > 70) {...}), in loop conditions, and in method arguments. Mastering operators and precedence gives you the power to write correct, readable Java code.
Identify the Operands and Operators
Look at the expression and separate it into the values (operands) and the symbols (operators). For example, in 10 + 5 * 2, the operands are 10, 5, and 2, and the operators are + and *. This step helps you see what you are working with.
Apply Operator Precedence
Determine which operator has higher precedence. Refer to the Java precedence table: parentheses highest, then multiplicative (*, /, %), then additive (+, -), then relational, then logical, then assignment. In 10 + 5 * 2, multiplication has higher precedence, so you evaluate 5 * 2 first, resulting in 10 + 10.
Handle Parentheses Explicitly
If the expression contains parentheses, evaluate the inner-most parentheses first. For example, (10 + 5) * 2 forces addition to happen first, giving 15 * 2 = 30. Parentheses override the default precedence.
Evaluate the Expression Left to Right for Same Precedence
When operators have the same precedence, evaluate them from left to right (most operators are left-associative). In 20 - 5 - 3, subtraction is left-associative, so 20 - 5 = 15, then 15 - 3 = 12. Assignment operators are right-associative: int a, b, c; a = b = c = 5; assigns 5 to c first, then b, then a.
Trace the Result and Check Side Effects
After computing the result, check if any operator changed a variable's value permanently (side effects). Increment/decrement operators (++, --) and assignment operators change variables. For example, int x = 5; int y = x++ + 2; After the expression, y is 7, but x becomes 6. Always note the final state of all variables involved.
An IT professional — say, a junior Java developer at an online store — uses operators and expressions daily. Here is a concrete scenario: the store runs a promotional campaign: '10% discount on all items over €50, but only for members who have been registered for more than one year.'
The developer writes a method that calculates the final price for each customer. They start by retrieving the item price (a double variable called itemPrice) and the customer's membership length in years (an int variable called membershipYears). The first step is to build a condition that checks if the price qualifies: itemPrice > 50.0. That is a relational expression that returns true or false. Then they check membership: membershipYears > 1. Both conditions must be true for the discount to apply, so they combine them with the logical AND operator (&&): itemPrice > 50.0 && membershipYears > 1.
If the condition is true, they apply the discount by calculating discountedPrice = itemPrice * 0.9. Here, * is an arithmetic operator, and = is the assignment operator storing the result in a new variable. They then combine the final price with shipping costs: finalPrice = discountedPrice + shippingCost. If the condition is false, they skip the discount and directly assign finalPrice = itemPrice + shippingCost.
Now, imagine the developer makes a mistake with precedence. They write: if (itemPrice > 50.0 && membershipYears > 1) { finalPrice = itemPrice * 0.9 + shippingCost; } They might expect the multiplication to happen first (which it does), but what if they intended to multiply the discounted price by 0.9, not the original price? Actually, this code is correct, but a more dangerous error could be: if (itemPrice > 50.0 && membershipYears = 1) — this is a common trap where a single = (assignment) is used instead of == (equality). That single = would assign the value 1 to membershipYears, and the expression would always evaluate to true (because assignment returns the assigned value, which is non-zero, and Java treats non-zero as true in a boolean context? Actually, assignment returns a value, but in Java, the condition of an if must be a boolean — and assigning an int to a boolean is a compile error. But if the variable were boolean, the assignment would work and always be true, causing a logical bug.
In a larger system, this kind of mistake leads to the store giving discounts to all members regardless of their registration length, costing the business money until the bug is found. Developers debug such issues by walking through expressions step by step, using print statements or a debugger to check the value of each sub-expression. They also use parentheses to make the intended order explicit: ((itemPrice > 50.0) && (membershipYears > 1)).
Compound assignment operators are also used frequently. For example, the developer might need to accumulate a running total: totalSales += itemPrice;. This is shorter and clearer than totalSales = totalSales + itemPrice;. In performance-critical code, these operators can also be slightly faster because they avoid an extra temporary variable.
Finally, when writing unit tests, the developer will write test cases that verify expressions produce the expected boolean results. For instance, a test for the discount rule might check that a €60 item with a 2-year membership results in a final price of €54 (€60 * 0.9 = €54, plus shipping). They write assertions like: assertEquals(54.0 + 5.0, calculateFinalPrice(60.0, 2, 5.0));. Every one of these tests relies on the correct behaviour of arithmetic, relational, and assignment operators.
The 1Z0-811 exam tests your understanding of operators and expressions in several specific ways. You will see questions that ask you to evaluate the result of a given expression, questions that test your knowledge of operator precedence, and questions that check whether you can identify the correct operator for a given task (e.g., 'Which operator would you use to check if two values are not equal?').
Here are the exact concepts the exam loves to test:
Precedence and associativity: You will be given an expression like int result = 5 + 3 * 2 - 1; and asked what value is stored in result. The correct answer is 10 (because 3 * 2 = 6, then 5 + 6 = 11, then 11 - 1 = 10). They also test that assignment has the lowest precedence. Example: int x = 5; x += 2 * 3; results in x being 11, not 21. They love to test the mixing of different operator types in one expression.
Increment/decrement operators: Pre-increment (++x) versus post-increment (x++) is a common trap. For example, int a = 5; int b = ++a; results in both a and b being 6. If you instead write int b = a++; then b is 5 and a is 6. The exam will ask you to trace the value of variables after a sequence of increments.
Logical operator short-circuiting: They might present code like boolean flag = (5 > 3) || (10 / 0 == 1); and ask if an exception occurs. Because || short-circuits and the left side is true, the right side (10 / 0) is never evaluated, so no ArithmeticException is thrown. The same concept applies to &&: if the left side is false, the right side is skipped.
Distinguishing = (assignment) from == (equality): This is a classic exam trap. They will write something like if (x = 5) { ... } and ask if it compiles. In Java, x = 5 is an assignment expression, and it returns the value 5, which is an int, not a boolean. The if condition must be a boolean, so this code does not compile. However, if x were a boolean variable, if (x = true) would compile and always be true, which is a logical bug the exam wants you to spot.
Modulus operator (%): Questions about remainders. For example, what is 7 % 3? Answer: 1. They might use negative numbers: -7 % 3 gives -1 in Java (because the sign of the result follows the sign of the dividend).
Compound assignment operators: You might see int y = 10; y %= 3; What is y? Answer: 1.
Common exam traps to watch for:
Assuming addition has higher precedence than multiplication.
Forgetting that assignment is right-associative (int a, b, c; a = b = c = 5; assigns 5 to c, then b, then a).
Mixing logical && and || without parentheses and misreading the result.
Thinking that relational operators can be chained like in maths: 5 < x < 10 is NOT valid Java; you must write x > 5 && x < 10.
Forgetting that the equality operator for primitives is ==, but for String objects you should use .equals() (though the exam focuses on primitives).
To prepare, practise evaluating expressions by hand, writing out each step. Use the mnemonic 'PEMDAS' but adapted for Java: parentheses, unary (++, --, !), multiplicative (*, /, %), additive (+, -), relational (<, >, <=, >=), equality (==, !=), logical AND (&&), logical OR (||), assignment (=, etc.). Many exam questions boil down to applying this order correctly.
An expression is any valid combination of values and operators that produces a single value.
Arithmetic operators (+, -, *, /, %) perform mathematical calculations on numeric operands.
Relational operators (==, !=, <, >, <=, >=) compare two values and return a boolean (true or false).
Logical operators (&&, ||, !) combine or invert boolean values to make complex decisions.
Assignment operators (=, +=, -=, etc.) store a value into a variable, and compound assignment operators combine arithmetic with assignment.
Operator precedence determines the order of evaluation: multiplication and division happen before addition and subtraction, unless parentheses override it.
Short-circuit evaluation means && and || may skip evaluating the second operand if the first already determines the result.
Use parentheses to make the intended order of operations explicit and improve code readability.
The equality operator == checks if values are equal, while = assigns a value — mixing them up creates bugs.
Pre-increment (++x) adds 1 before using the value; post-increment (x++) uses the value before adding 1.
These come up on the exam all the time. Here's how to tell them apart.
= (Assignment)
Stores a value into a variable.
Returns the assigned value (e.g., int).
Right-associative (a = b = 5).
== (Equality)
Compares two values for equality.
Returns a boolean (true or false).
Left-associative (a == b == c is invalid for booleans and numbers mixed).
Pre-increment (++x)
Increments x first, then uses the new value.
If x = 5, --x gives 6 and x becomes 6.
Used when you need the updated value immediately.
Post-increment (x++)
Uses the current value of x, then increments.
If x = 5, x++ results in 5, then x becomes 6.
Used when you need the original value before incrementing.
&& (Logical AND)
Short-circuits: if left is false, right is not evaluated.
Guarantees both operands are boolean.
Commonly used in conditions for clarity and efficiency.
& (Bitwise AND - for booleans)
Always evaluates both operands, no short-circuit.
Can also be used on integer bits, but for booleans it works.
Rarely used with booleans; && is preferred.
Arithmetic Operators (+, -, *, /, %)
Produce a numeric result (int, double, etc.).
Have higher precedence than logical and assignment operators.
Used for calculations.
Relational Operators (==, !=, <, >, <=, >=)
Produce a boolean result (true or false).
Have lower precedence than arithmetic but higher than logical.
Used for comparisons and decisions.
Mistake
The assignment operator = means 'is equal to' like in maths.
Correct
In Java, = stores a value into a variable. The equality operator == is used to compare whether two values are equal.
In everyday language, '=' is used to mean equality, so beginners naturally carry that meaning into code.
Mistake
The % operator gives the quotient of division.
Correct
The % (modulus) operator gives the remainder of a division. For example, 10 % 3 is 1 (the remainder), not 3 (the quotient).
The word 'modulus' sounds similar to 'modulo' and beginners often confuse it with simple division.
Mistake
The expression (x++ + ++x) is always the same as (2 * x + 1).
Correct
The result depends on the initial value of x and the order of evaluation, which is side-effect-driven. For x = 5, x++ + ++x = 5 + 7 = 12, not 11. The right side is evaluated after the left side increments x, so x changes mid-expression.
Beginners assume arithmetic is purely mathematical, but Java evaluates expressions step by step, and increment operators change the variable's value during evaluation.
Mistake
Logical operators && and || always evaluate both sides of the expression.
Correct
They use short-circuit evaluation: && stops if the left side is false, and || stops if the left side is true. This means the right side may never run.
The word 'and' in everyday language implies both parts happen, but in Java the right side is skipped when it cannot change the result.
Mistake
You can chain relational operators like in maths: 0 < x < 10.
Correct
Java does not allow chaining. You must write x > 0 && x < 10.
In algebra, 'a < b < c' is common notation, but Java parses it as (a < b) < c, comparing a boolean to a number, which causes a compile error.
Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.
== is a relational operator that checks if two values are equal, returning true or false. = is the assignment operator that stores a value into a variable. Using = when you mean == is a common bug.
Yes, Java uses short-circuit evaluation for && (AND) and || (OR). For &&, if the left condition is false, the right side is not evaluated. For ||, if the left condition is true, the right side is skipped. This can prevent errors like division by zero.
Use the mnemonic 'PUMA' for the main categories: Parentheses, Unary (++, --, !), Multiplicative (*, /, %), Additive (+, -). Then come Relational, Equality, Logical (&& then ||), and finally Assignment. Practise by evaluating expressions step by step.
The % operator returns the remainder of a division. For example, 10 % 3 gives 1. It is commonly used to check if a number is even (number % 2 == 0) or to wrap values around a range.
No, relational operators (like <, >, <=, >=) work only on numeric types (int, double, etc.) and types that implement Comparable, but not on boolean. For booleans, you use == or != to compare, or logical operators like && and || to combine them.
Java will still evaluate the expression using its built-in precedence rules, but the result may not be what you intend. This leads to logic bugs that are hard to spot. Always use parentheses to clarify your intended order, especially when mixing different operator types.
You've finished Operators, Expressions, and Operator Precedence. Continue through the 1Z0-811 study guide to build a complete picture of the exam.
Done with this chapter?