Operators
Operators are the verbs of programming — they perform actions on your data. Learn how to do math, compare values, and combine conditions.
Learn Operators in our free Java course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Java course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
What You'll Learn in This Lesson
1️⃣ What Are Operators? (Plain English)
An operator is a symbol that tells Java to perform an action on your data. You already know some from math class: + , - , * .
💡 Analogy: Think of operators like buttons on a calculator. Each button performs a specific action. But Java's calculator has some extra buttons (like % and && ) and some quirks (like integer division) that you need to know about.
- Arithmetic: Do math ( + , - , * , / , % )
- Comparison: Compare values ( == , != , > , < )
- Logical: Combine true/false ( && , || , ! )
- Assignment: Store values ( = , += , -= )
2️⃣ Arithmetic Operators — Doing Math
These work mostly like you'd expect from math class, with one big exception:
This is the #1 math gotcha in Java. When you divide two int values, Java performs integer division — it throws away the decimal part completely (no rounding!).
3️⃣ The Modulo Operator (%) — The Most Underrated Operator
The % (modulo) operator gives you the remainder after division. It seems simple, but it's incredibly powerful:
Pattern
Code
What It Does
Check even/odd
n % 2 == 0
True if n is even
Get last digit
n % 10
Returns last digit of n
Wrap around
index % arrayLength
Keeps index in bounds
Time conversion
seconds % 60
Remaining seconds
💡 Example: Converting 3725 seconds to minutes and seconds:
4️⃣ Comparison Operators — Asking Yes/No Questions
Comparison operators compare two values and return true or false . They're essential for making decisions in your programs (which you'll do in the next lesson with if statements).
5️⃣ Logical Operators — Combining Conditions
Logical operators let you combine multiple true/false conditions into one. There are three:
Operator
Name
Meaning
Example
&&
AND
Both must be true
age > 18 && hasID
||
OR
At least one must be true
isVIP || hasCoupon
!
NOT
Flips true ↔ false
!isBlocked
💡 Short-Circuit Evaluation: In false && anything , Java never evaluates anything because AND already can't be true. This is useful for safety:
6️⃣ Assignment Operators — Shortcuts for Changing Values
Instead of writing x = x + 5 , Java has shortcuts:
When used alone on a line ( i++; ), pre and post increment do the same thing. The difference only matters when used inside another expression. When in doubt, put them on their own line.
7️⃣ Operator Precedence — Order Matters!
Just like in math, Java has rules about which operations happen first. Multiplication happens before addition unless you use parentheses:
Priority
Operators
Category
1 (highest)
() ++ -- !
Parentheses, unary
2
* / %
Multiplication, division
3
+ -
Addition, subtraction
4
< > <= >=
Comparison
5
== !=
Equality
6
Logical AND
7
Logical OR
8 (lowest)
= += -= *= /=
Assignment
Don't try to memorize this whole table. Instead, use parentheses whenever you're unsure. (a + b) * c is clearer than relying on precedence rules. Your future self will thank you.
8️⃣ The == vs .equals() Trap (Strings)
This trips up every Java beginner. With numbers, == works perfectly. With Strings, it's dangerous:
💡 Why? For primitives (int, double, boolean), == compares the actual values. For objects (String, arrays), == compares whether they're the same object in memory — not whether they have the same content. Use .equals() for content comparison.
Common Beginner Mistakes
- ❌ = vs ==: = is assignment ("put this value in"), == is comparison ("are these equal?"). Writing if (x = 5) is wrong — use if (x == 5)
- ❌ Integer division: 1 / 3 is 0 , not 0.33 . Always make at least one operand a double when you want decimals
- ❌ String comparison with ==: == compares memory addresses for objects. Always use .equals() for String content comparison
- ❌ Division by zero: int x = 10 / 0; crashes with ArithmeticException . (But 10.0 / 0 gives Infinity — different behaviour!)
- ❌ Forgetting precedence: 2 + 3 * 4 is 14, not 20. Use parentheses when in doubt: (2 + 3) * 4
💡 Pro Tips
- 💡 Use parentheses liberally: Even when not needed, they make your code clearer. (a * b) + c is easier to read than a * b + c
- 💡 Modulo for cycling: index % arraySize automatically wraps around — perfect for circular buffers, rotating through options, etc.
- 💡 Short-circuit for safety: obj != null && obj.method() prevents NullPointerException. Java won't call the method if obj is null
- 💡 Ternary operator preview: int max = (a > b) ? a : b; is a one-line if/else. You'll learn this in the next lesson!
📋 Quick Reference
Precedence
Unary
++ -- ! (type)
Highest
Multiplicative
High
Additive
Medium
Relational
< > <= >= instanceof
Medium-low
Low
Lower
Lower still
= += -= *= /= %=
Lowest
🎉 Lesson Complete!
Great work! You now understand all of Java's core operators — arithmetic, comparison, logical, and assignment. You know the integer division trap, how modulo works in real scenarios, and why == vs .equals() matters for Strings.
Next up: Control Flow — use these operators inside if statements and switch cases to make your programs decide and react to different situations.
Practice quiz
What does the modulo operator % return?
- The quotient of a division
- The largest of two numbers
- The remainder after division
- A boolean
Answer: The remainder after division. % gives the remainder after division, e.g. 10 % 3 is 1.
What is the value of 10 % 3?
- 1
- 3
- 0
- 3.33
Answer: 1. 10 divided by 3 is 3 with a remainder of 1, so 10 % 3 is 1.
What does 2 + 3 * 4 evaluate to?
- 20
- 24
- 9
- 14
Answer: 14. Multiplication has higher precedence than addition: 3 * 4 = 12, then + 2 = 14.
What is the difference between = and ==?
- They are identical
- = assigns a value, == compares for equality
- = compares, == assigns
- Both compare values
Answer: = assigns a value, == compares for equality. = is assignment ('put this value in'); == is comparison ('are these equal?').
With int a = 5; int b = a++; what are b and a afterward?
- b = 5, a = 6
- b = 6, a = 6
- b = 5, a = 5
- b = 6, a = 5
Answer: b = 5, a = 6. Post-increment uses the value first (b = 5), then increments a to 6.
What is the result of 7.0 / 2?
- 3
- 4
- 3.5
- 3.0
Answer: 3.5. Because one operand is a double, this is floating-point division, giving 3.5.
For the && (AND) operator, when is the result true?
- When at least one side is true
- Only when both sides are true
- When both sides are false
- Always
Answer: Only when both sides are true. && is true only when both operands are true.
What does !hasLicense evaluate to when hasLicense is true?
- true
- null
- An error
- false
Answer: false. The NOT operator ! flips the boolean, so !true is false.
After int score = 100; score += 10; score *= 2; what is score?
- 110
- 220
- 120
- 200
Answer: 220. score becomes 110, then 110 * 2 = 220.
Why should you avoid == when comparing String objects?
- It is slower
- It does not compile
- It compares memory addresses, not text content
- It only works on numbers
Answer: It compares memory addresses, not text content. For objects, == compares references (memory addresses); use .equals() to compare actual text.
Continue this course
- Previous: Previous Lesson
- Next: Next Lesson