Operators in Java Explained (with Examples)
Learn Java operators the easy way: arithmetic, assignment, comparison, and logical operators, plus increment/decrement and precedence — explained simply with examples.
Now that you can store data in variables, let's make your programs actually do things with that data — add it, compare it, and make decisions with it. That's what operators are for. They're the symbols that perform operations on values, and you already know most of them from basic maths.
1. Arithmetic operators
These do the maths you'd expect:
| Operator | Meaning | Example (a=10, b=3) |
|---|---|---|
+ |
Addition | a + b → 13 |
- |
Subtraction | a - b → 7 |
* |
Multiplication | a * b → 30 |
/ |
Division | a / b → 3 |
% |
Remainder (modulo) | a % b → 1 |
Two things beginners trip on:
- Integer division truncates.
10 / 3is3, not 3.33 — because both areint. To get decimals, use adouble:10.0 / 3→ 3.33. %gives the remainder. It's incredibly handy — e.g.n % 2 == 0checks ifnis even.
2. Assignment operators
= assigns a value. The others are handy shortcuts:
int x = 10; x += 5; // same as x = x + 5; -> 15 x -= 3; // x = x - 3; -> 12 x *= 2; // x = x * 2; -> 24
3. Comparison (relational) operators
These compare two values and always give a boolean (true or false):
| Operator | Meaning |
|---|---|
== |
Equal to |
!= |
Not equal to |
> < |
Greater / less than |
>= <= |
Greater/less than or equal |
Watch out: = assigns, but == compares. Mixing them up is a classic beginner bug. (Note: for comparing text/Strings, you use .equals() instead of == — we'll cover why in a later lesson.)
4. Logical operators
These combine boolean values — essential for decisions:
&&(AND) — true only if both sides are true.||(OR) — true if either side is true.!(NOT) — flips true to false and vice versa.
int age = 20; boolean canVote = age >= 18 && age < 120; // true boolean isWeekend = false; System.out.println(!isWeekend); // true
5. Increment and decrement
++ adds 1 and -- subtracts 1 — you'll see these constantly, especially in loops:
int count = 5; count++; // now 6 (same as count = count + 1) count--; // now 5
A quick word on precedence
Just like in maths, operators have an order: *, /, % happen before + and -, and you can use parentheses to control the order and make your intent clear:
int result = 2 + 3 * 4; // 14 (multiplication first) int clear = (2 + 3) * 4; // 20 (parentheses first)
When in doubt, add parentheses — clearer code beats clever code.
Early on, the two bugs that caught me most were integer division (1/2 giving 0) and typing = when I meant ==. If your maths ever looks "wrong," check those two first — it's almost always one of them.
Key takeaways
- Arithmetic:
+ - * / %; integer division truncates, and%gives the remainder. - Comparison operators give a boolean;
=assigns while==compares. - Logical
&&,||,!combine booleans for decisions. ++/--add or subtract 1; use parentheses to control precedence.
← Previous: Lesson 4 — Variables and Data Types
Next: Lesson 6 — Input and Output in Java (Scanner) →
Frequently Asked Questions
What are the types of operators in Java?
The main types are arithmetic (+ - * / %), assignment (= += -= etc.), comparison/relational (== != > < >= <=), logical (&& || !), and increment/decrement (++ --). Comparison and logical operators produce boolean true/false values.
What does the % operator do in Java?
The % (modulo) operator gives the remainder of a division. For example 10 % 3 is 1. It is very useful, for example n % 2 == 0 checks whether a number n is even.
What is the difference between = and == in Java?
A single = assigns a value to a variable, while == compares two values and returns a boolean (true or false). Confusing the two is a common beginner bug. Note that to compare text/Strings you use .equals() instead of ==.
Why does 10 / 3 give 3 in Java?
Because both 10 and 3 are integers, Java does integer division and truncates the decimal part, giving 3. To get a decimal result like 3.33, make at least one value a double, for example 10.0 / 3.