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
️ % is massively used in DSA — even/odd check, circular arrays, divisibility.
2️⃣ Unary Operators
java
QUBITS OF DPK
3️⃣ Assignment Operators
java
QUBITS OF DPK
4️⃣ Relational Operators (always return boolean)
java
QUBITS OF DPK
5️⃣ Logical Operators
java
QUBITS OF DPK
6️⃣ Bitwise Operators
java
QUBITS OF DPK
7️⃣ Shift Operators
java
QUBITS OF DPK
8️⃣ Ternary Operator
java
QUBITS OF DPK
Short Circuit Evaluation — Critical for Interviews
java
QUBITS OF DPK
XOR Magic (DSA)
java
QUBITS OF DPK
️ All Traps Together
java
QUBITS OF DPK
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
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.