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.
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.
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!).
The % (modulo) operator gives you the remainder after division. It seems simple, but it's incredibly powerful:
💡 Example: Converting 3725 seconds to minutes and seconds:
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).
Logical operators let you combine multiple true/false conditions into one. There are three:
💡 Short-Circuit Evaluation: In false && anything , Java never evaluates anything because AND already can't be true. This is useful for safety:
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.
Just like in math, Java has rules about which operations happen first. Multiplication happens before addition unless you use parentheses:
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.
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.
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.