Nearby lessons
17 of 125Java - Operators (All Seven Types)
- The small building blocks of Java: tokens
- All 8 primitive data types with their ranges
- Type casting — implicit and explicit
- Control statements: if, switch, loops
- Arrays — single, double and jagged
- Variable length arguments (var-args)
Operators (All Seven Types) is a core concept of the Java language. This lesson explains The Seven Types of Operators with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
The Seven Types of Operators
The classic material groups operators into seven families. Make a small chart in your mind:
| Operator family | Symbols |
|---|---|
| Arithmetic | + - * / % ++ -- |
| Assignment | = += -= *= /= %= |
| Comparison (relational) | == != < > <= >= |
| Boolean logical | & | ^ (on true/false) |
| Bitwise logical | & | ^ << >> (on binary values) |
| Short-circuit | && || |
| Ternary | condition ? value1 : value2 |
Increment and Decrement — ++ and --
a++ (post-increment) uses the old value first, then increases. ++a (pre-increment) increases first, then uses the new value.
Classic tricky question: ++a - ++a with a = 5 gives -1 because the second ++a is evaluated before the subtraction, and both operate on the same variable.
The Seven Types of Operators
Logical Operators — Truth Tables
For booleans, & (AND), | (OR) and ^ (XOR) follow these rules (T = true, F = false):
| A | B | A & B | A | B | A ^ B |
|---|---|---|---|---|
| T | T | T | T | F |
| T | F | F | T | T |
| F | T | F | T | T |
| F | F | F | F | F |
Memorise: AND is true only when both are true. OR is true when at least one is true. XOR is true when the two are different.
Bitwise Operators and Shifts
The same & | ^ also work on numbers, operating bit by bit on their binary form. << shifts bits left (multiply by 2), >> shifts right (divide by 2).
The Seven Types of Operators
Short-Circuit Operators — & vs &&, | vs ||
This is a favourite interview question. The double operators (&&, ||) are smart: if the first part decides the answer, they skip the second part. The single operators (&, |) always evaluate both sides.
| Rule | Example |
|---|---|
| || — if first operand is true, second is NOT evaluated | (c++ == 10) || (d++ == 10) -> d is not incremented |
| | — even if first is true, second is evaluated | (a++ == 10) | (b++ == 10) -> b is incremented |
| && — if first operand is false, second is NOT evaluated | (c++ != 10) && (d++ != 10) -> d is not incremented |
| & — even if first is false, second is evaluated | (a++ != 10) & (b++ != 10) -> b is incremented |
The Seven Types of Operators
Why do short-circuit operators exist? Performance — if the answer is already known from the first operand, evaluating the second is wasted work.
- Tokens are the smallest pieces of a program: identifiers, literals, keywords, operators, separators.
- Java has 8 primitive types: byte, short, int, long, float, double, char, boolean.
- Widening casting is automatic and safe; narrowing casting needs brackets and may lose data.
- if / switch decide which path runs; for / while / do-while repeat code; break and continue control loops.
- Arrays hold many same-type values; index starts at 0; size is fixed; for-each loop reads them simply.
- Var-args (int... x) lets a method take any number of arguments.