Cosmic Module

J

Qubits of DPK

March 14, 2026

Core Java

Layman Explanation

Operators are action symbols. They tell Java what to DO with values — add them, compare them, flip them, combine them.

8 Types of Operators

1️⃣ Arithmetic Operators

java
QUBITS OF DPK
1int a = 10, b = 3;
2a + b   // 13
3a - b   // 7
4a * b   // 30
5a / b   // 3  ← INTEGER division! Not 3.33
6a % b   // 1  (remainder)
% is massively used in DSA — even/odd check, circular arrays, divisibility.

2️⃣ Unary Operators

java
QUBITS OF DPK
1int x = 5;
2x++    // post: use 5, then x becomes 6
3++x    // pre:  x becomes 7, use 7
4x--    // post decrement
5--x    // pre decrement
6!true  // false

3️⃣ Assignment Operators

java
QUBITS OF DPK
1x += 5   // x = x + 5
2x -= 5   // x = x - 5
3x *= 5   // x = x * 5
4x /= 5   // x = x / 5
5x %= 5   // x = x % 5

4️⃣ Relational Operators (always return boolean)

java
QUBITS OF DPK
15 == 5   // true
25 != 3   // true
35 > 3    // true
45 < 3    // false
55 >= 5   // true
65 <= 4   // false

5️⃣ Logical Operators

java
QUBITS OF DPK
1true && false   // false (AND)
2true || false   // true  (OR)
3!true           // false (NOT)

6️⃣ Bitwise Operators

java
QUBITS OF DPK
15 & 3    // 1  (AND bits)
25 | 3    // 7  (OR bits)
35 ^ 3    // 6  (XOR bits)
4~5       // -6 (NOT bits)

7️⃣ Shift Operators

java
QUBITS OF DPK
15 << 1   // 10  (multiply by 2)
220 >> 1  // 10  (divide by 2)

8️⃣ Ternary Operator

java
QUBITS OF DPK
1String result = (age >= 18) ? "Adult" : "Minor";

Short Circuit Evaluation — Critical for Interviews

java
QUBITS OF DPK
1// Safe null check — short circuit stops at null check
2if (name != null && name.equals("Deepak")) { }   // ✅ safe order
3if (name.equals("Deepak") && name != null) { }   // ❌ NullPointerException risk!

XOR Magic (DSA)

java
QUBITS OF DPK
15 ^ 5 = 0    // number XOR itself = 0
25 ^ 0 = 5    // number XOR 0 = itself
3// Used in: Find single non-duplicate element

️ All Traps Together

java
QUBITS OF DPK
1// Trap 1 — Integer division
210 / 3 = 3  (not 3.33)
3
4// Trap 2 — Post vs pre increment with carry
5int x = 5;
6System.out.println(x++);  // 5
7System.out.println(++x);  // 7 (not 6!)
8
9// Trap 3 — == on Strings
10"hello" == "hello"          // unreliable
11"hello".equals("hello")     // always correct ✅
12
13// Trap 4 — Assignment vs comparison
14if (x = 5)   // ❌ assignment
15if (x == 5)  // ✅ comparison
16
17// Trap 5 — Casting before division
18double d = 10 / 3;           // 3.0
19double d = (double) 10 / 3;  // 3.333 ✅

Interview Answer

"Java has 8 operator types. Arithmetic for math — watch integer division. Unary for increment/decrement — pre increments first, post uses first. Assignment shorthand. Relational returns boolean — never == for Strings. Logical with short-circuit evaluation — order matters for null safety. Bitwise at bit level — XOR used heavily in DSA. Shift for fast multiply/divide by powers of 2. Ternary for one-line conditionals."

Interview Questions & MAANG-Level Answers

Q1. What is the output of x++ vs ++x when x=5?
x++ (post-increment) uses the current value first, then increments. So if x=5, System.out.println(x++) prints 5 and then x becomes 6. ++x (pre-increment) increments first, then uses. So if x=5, System.out.println(++x) makes x=6 and prints 6. Critical trap: if x=5, then System.out.println(x++); System.out.println(++x); prints 5 then 7 (not 6!) because after first line x=6, then ++x makes it 7.
Q2. What is short circuit evaluation and why does it matter?
In &&, if the left side is false, Java skips evaluating the right side (result can never be true). In ||, if left side is true, right side is skipped. This matters critically for null safety:
java
QUBITS OF DPK
1if (str != null && str.equals("hi"))  // SAFE: null check first
2if (str.equals("hi") && str != null)  // DANGER: NPE if str is null
Always put the null/bounds check FIRST in conditions.
Q3. What is 5 ^ 5 and where is XOR used in DSA?
5 ^ 5 = 0. XOR of any number with itself is 0. Also 5 ^ 0 = 5 (XOR with 0 returns the number). This is used in the classic DSA problem "Find the single non-duplicate element in an array" — XOR all elements together, duplicates cancel out (x^x=0), leaving only the unique element. O(n) time, O(1) space. Also used in bit manipulation, encryption basics, and in-place swap without temp variable.
Q4. Why should you never use == to compare Strings?
== compares references (memory addresses), not content. String literals in the pool may share the same reference (so == works by coincidence), but new String("hello") creates a new heap object, so == returns false even though content is identical. Always use .equals() for content comparison. Example: "hello" == new String("hello") is false, but "hello".equals(new String("hello")) is true.
Q5. What is the difference between & and &&?
& is bitwise AND — operates on every bit of the integer. && is logical AND — works on booleans with short-circuit evaluation. With &&, right side is NOT evaluated if left is false. With &, both sides are ALWAYS evaluated. Example: if (obj != null & obj.method()) — using & here can throw NPE because both sides always evaluate. Use && for conditions, & only for bit manipulation.