Module 3: Operators, Expressions, and Strings
Expressions and Operator Precedence
Read mixed expressions more accurately by learning how Java decides what to evaluate first.
Author
Java Learner Editorial Team
Reviewer
Technical review by Java Learner
Last reviewed
2026-04-16
Java version
Java 25 LTS
Learning goals
- Recognize that multiplication runs before addition in normal expressions
- Use parentheses to make your intent obvious
- Avoid confusion when strings and numbers appear together
Operator precedence: Java follows an order of operations. For example, multiplication happens before addition unless parentheses say otherwise.
Parentheses help readers: Even when you know the precedence rules, parentheses often make the code easier to read and harder to misunderstand.
Strings change the result shape: In concatenation, once Java starts building a string, later values may join as text instead of continuing numeric math.
Why this matters: Precedence bugs are common in beginner programs because the code looks right at a glance.
Runnable examples
Math precedence
public class Main {
public static void main(String[] args) {
System.out.println(2 + 3 * 4);
System.out.println((2 + 3) * 4);
}
}Expected output
14 20
String concatenation order
public class Main {
public static void main(String[] args) {
System.out.println("Total: " + 2 + 3);
System.out.println("Total: " + (2 + 3));
}
}Expected output
Total: 23 Total: 5
Common mistakes
Assuming `"Total: " + 2 + 3` adds the numbers first
Use parentheses when you want the math first: `"Total: " + (2 + 3)`.
Mini exercise
Write one expression that prints `11` and another that prints `Result: 11`.
Summary
- Order matters in expressions.
- Parentheses are often the clearest option.
Next step
Module 4 turns these boolean and comparison ideas into real decisions with `if`, `else if`, and `switch`.
Sources used