Java Operator Fundamentals — Quick & Clear

Short examples and outputs to make operators easy for students.

1. Precedence & Associativity

Operator precedence determines the order in which operators are evaluated. Associativity decides evaluation direction when precedence is same.

Example: multiplication has higher precedence than addition.

int x = 10 + 5 * 2; System.out.println(x); // multiplication first
20

2. Assignment Operator ( = )

Assignment stores a value into a variable. Right-hand side is evaluated first, then assigned to the left-hand variable.

int a = 10; int b = a + 5; System.out.println(b);
15

3. Basic Arithmetic Operators

Use +, -, *, /, % for arithmetic on numbers.

int a = 10, b = 3; System.out.println(a + b); System.out.println(a % b);
13
1

4. Increment (++) and Decrement (--)

Prefix (++x) updates the value then uses it. Postfix (x++) uses the current value then updates it.

int x = 5; System.out.println(++x); // 6 System.out.println(x++); // 6 (old value) System.out.println(x); // 7 (after postfix)
6
6
7

5. Ternary Operator

Short form of if-else: condition ? valueIfTrue : valueIfFalse. Great for concise expressions.

int age = 20; String res = (age >= 18) ? "Adult" : "Minor"; System.out.println(res);
Adult

6. Relational Operators

Compare values using >, <, >=, <=, ==, !=. They return boolean results.

int a = 10, b = 20; System.out.println(a < b); // true
true

7. Boolean Logical Operators

Use && (AND), || (OR), and ! (NOT) to combine boolean expressions.

boolean x = true, y = false; System.out.println(x && y); System.out.println(x || y);
false
true

8. Bitwise Logical Operators

Operate at bit-level: &, |, ^, ~, shifts <<, >>. Useful in low-level tasks and optimizations.

int x = 5; // 0101 int y = 3; // 0011 System.out.println(x & y); // 0001 -> 1 System.out.println(x | y); // 0111 -> 7
1
7

Mini Precedence Reminder

High to low (short): () → ++/-- → * / % → + - → << >> → & → ^ → | → && → || → ?: → = =

Practice Questions – Click to Reveal Answers

1️⃣ Which operator has higher precedence: * or +?
The multiplication operator (*) has higher precedence.
2️⃣ What does a = b + 5 do?
It evaluates b + 5 first, then assigns the result to a.
3️⃣ What is the % operator?
The modulus operator; it returns the remainder after division.
4️⃣ If x = 5, what is the output of ++x?
It outputs 6 because prefix increments before use.
5️⃣ Write a ternary expression to check if x is positive.
String r = (x > 0) ? "Positive" : "Not Positive";
6️⃣ What does == check for primitives?
It checks if the two primitive values are equal.
7️⃣ What is the result of true && false?
It evaluates to false.
8️⃣ What is the bitwise result of 5 & 3?
Binary: 0101 & 0011 = 0001 → 1.
9️⃣ Explain postfix x++ behavior.
It returns the current value of x, then increments x by 1.
🔟 Which operator short-circuits: && or &?
The conditional && short-circuits (doesn't evaluate right-hand side if left is false); bitwise & always evaluates both sides.

Quick Summary