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
JCode Cell
1int a = 10;
2System.out.println(a); // 10
3System.out.println(a++); // 10 (post - print first, then add)
4System.out.println(++a); // 12 (pre - add first, then print)
5System.out.println(a--); // 12 (post - print first, then subtract)
6System.out.println(--a); // 10 (pre - subtract first, then print)
7System.out.println(a); // 10
Output
10 10 12 12 10 10

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
JCode Cell
1int a = 5;
2System.out.println(++a - ++a);
3// first ++a -> a=6 ; second ++a -> a=7 ; then 6 - 7 = -1

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
JCode Cell
1int a = 10; // binary 1010
2int b = 2; // binary 0010
3 
4a & b -> 0010 = 2
5a | b -> 1010 = 10
6a ^ b -> 1000 = 8
7a << b -> 10 << 2 = 40 (add two 0s at the right)
8a >> b -> 10 >> 2 = 2 (remove two bits from the right)

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
JCode Cell
1class ShortCircuit {
2 public static void main(String[] args) {
3 int a = 10, b = 10, c = 10, d = 10;
4 if ((a++ == 10) | (b++ == 10)) { } // both run: a=11, b=11
5 if ((c++ == 10) || (d++ == 10)) { } // second skipped: c=11, d=10
6 System.out.println(a + " " + b + " " + c + " " + d);
7 }
8}
Output
11 11 11 10
📝 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