Skip to main content

Chapter 4: Operators in Java – Deep & Clear Explanation

Published 4 min readMarkdown
Chapter 4: Operators in Java – Deep & Clear Explanation

Operators are the actions you perform on data.
If variables are nouns, then operators are verbs.

Every meaningful Java program depends on operators—whether it’s calculation, comparison, decision-making, or bit manipulation. A strong understanding of operators is non-negotiable for both exams and interviews.


Classification of Java Operators

Java operators are grouped into the following categories:

  • Arithmetic Operators

  • Bitwise Operators

  • Relational Operators

  • Logical Operators

  • Assignment Operators

  • Conditional (Ternary) Operator

  • Operator Precedence & Short-Circuiting

We will explore each category deeply, focusing on rules, pitfalls and real-world relevance.


1️⃣ Arithmetic Operators

Used for mathematical calculations.

Operators:
+ - * / %

🔴 Rule 1 (Very Important)

Arithmetic operators work only on numeric data types.

✔ int, float, double, byte, short, char
❌ boolean is NOT allowed

🔥 Char Is Numeric

char c = 'A';  // ASCII value = 65
System.out.println(c + 1); // 66

In Java, char is internally treated as an unsigned integer.


⚠ Integer Division (Exam + Bug Source)

System.out.println(5 / 2);   // 2 ❌
System.out.println(5.0 / 2); // 2.5 ✅

Golden Rule:

  • If both operands are integers, the result is an integer

  • Decimal part is discarded, not rounded


2️⃣ Modulus Operator %

Returns the remainder, not the quotient.

10 % 3 = 1
10.5 % 3 = 1.5

🔥 Real-World Uses

  • Even / Odd check → num % 2

  • Cyclic operations

  • Hashing & wrapping logic


3️⃣ Compound Assignment Operators

+= -= *= /= %=

a = a + 4;  // normal
a += 4;     // compound

🚨 Interview-Favorite Difference

byte b = 10;
b = b + 1;   // ❌ Compile-time error
b += 1;      // ✅ Works

Why?

  • b + 1 → promoted to int

  • += performs implicit casting

⚠ This convenience can hide bugs—use carefully.


4️⃣ Increment & Decrement Operators

Two forms:

  • Prefix: ++x

  • Postfix: x++

🔥 Golden Rule

FormWhen Increment Happens
++xBefore value is used
x++After value is used

Example

int x = 42;
int y = ++x; // x=43, y=43

int x = 42;
int y = x++; // y=42, x=43

👉 Final x is same
👉 y is different
👉 Major confusion source → master this


5️⃣ Bitwise Operators (Advanced but Powerful)

Operate at the bit level.

Applicable to:
byte, short, int, long, char

Operators:
& | ^ ~ << >> >>>


6️⃣ Two’s Complement (Very Important Concept)

Java stores negative numbers using two’s complement.

Example: storing -42

  1. 42 → 00101010

  2. Invert bits → 11010101

  3. Add 1 → 11010110

Why Java Uses It?

  • No negative zero

  • Faster arithmetic

  • Hardware-friendly

🔴 MSB (Sign Bit):

  • 0 → Positive

  • 1 → Negative


7️⃣ Bitwise Logical Operators

& (AND)

1 only if both bits are 1

| (OR)

1 if any bit is 1

^ (XOR – Very Important)

1 if exactly one bit is 1

XOR Properties:

  • a ^ a = 0

  • a ^ 0 = a

🔥 Used in encryption, toggling bits

~ (NOT)

Flips all bits

~42 = -43

8️⃣ Shift Operators

<< Left Shift

x << n  // x * (2^n)

⚠ Shifting into sign bit can make a number negative.

>> Right Shift (Signed)

Preserves sign bit

-8 >> 1 = -4

>>> Unsigned Right Shift

Always fills with 0

-1 >>> 24 = 255

⚠ Meaningful only for int and long


9️⃣ Relational Operators

== != < > <= >=

✔ Result is always boolean

🚫 Java ≠ C/C++

if (x)        // ❌ Invalid
if (x != 0)  // ✅ Correct

Java does not treat numbers as booleans.


🔟 Logical Operators (Boolean Only)

&& || ! ^

Used for decision-making and control flow.


1️⃣1️⃣ Short-Circuit Operators (Critical)

&& and ||

Right side executes only if required.

if (denom != 0 && num / denom > 10) // Safe

Using & here would cause division by zero.

When NOT to Short-Circuit

if (c == 1 & e++ < 100)

Here e++ must execute every time.


1️⃣2️⃣ Assignment Operator =

Chain Assignment

x = y = z = 100;

Works because = returns a value.


1️⃣3️⃣ Ternary Operator ?:

Compact replacement for simple if-else.

result = condition ? value1 : value2;

Example

int abs = x < 0 ? -x : x;

✔ Clean
✔ Fast
✔ Readable (when simple)


1️⃣4️⃣ Operator Precedence (Exam Trap)

a >> b + 3

Evaluated as:

a >> (b + 3)

Best Practice

  • Use parentheses for clarity

  • Avoid ambiguity

  • Parentheses do NOT hurt performance


No comments yet. Be the first to comment.