Nearby lessons

17 of 125

Java - Operators (All Seven Types)

📌 What You Will Learn
  • 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 familySymbols
Arithmetic+ - * / % ++ --
Assignment= += -= *= /= %=
Comparison (relational)== != < > <= >=
Boolean logical& | ^ (on true/false)
Bitwise logical& | ^ << >> (on binary values)
Short-circuit&& ||
Ternarycondition ? 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.

Example01
Loading editor…

The Seven Types of Operators

In simple words: `a++` uses the old value first and then increases; `++a` increases first and then uses the new value. So println(a++) prints the old value, while println(++a) prints the new value.

Logical Operators — Truth Tables

For booleans, & (AND), | (OR) and ^ (XOR) follow these rules (T = true, F = false):

ABA & BA | BA ^ B
TTTTF
TFFTT
FTFTT
FFFFF

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).

Example02
Loading editor…

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.

RuleExample
|| — 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
Example03
Loading editor…

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.

Example04
Loading editor…
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1

Finished this lesson?

Mark it as complete to track your progress and see technology completion stats.