➕ Java Operators — Arithmetic, Relational, Logical and Bitwise
Complete guide to Java operators — arithmetic, relational, logical, bitwise, assignment, ternary and instanceof. Learn precedence and short-circuit evaluation with examples.
Operators act on values. Java's set is similar to C/C++.
➕ Arithmetic
+ - * / % ++ --. Integer division truncates; % is remainder. + also concatenates Strings.
🟰 Relational
== != > < >= <= — all return boolean.
🔁 Logical
&& (AND), || (OR), ! (NOT). They short-circuit — a() && b() skips b() if a() is false. Use this to guard null checks.
🔬 Bitwise & shift
& | ^ ~ << >> >>> (unsigned right shift, Java-specific).
❓ Ternary & instanceof
String s = (age >= 18) ? "adult" : "minor";
if (obj instanceof String str) { // pattern matching, Java 16+
System.out.println(str.length());
}💡 Precedence
Unary > multiplicative > additive > shift > relational > equality > bitwise > logical > ternary > assignment. When unsure, parenthesize.
💻 Code Examples
Short-circuit guards a null check
String name = null;
if (name != null && name.length() > 0) {
System.out.println("has name");
} else {
System.out.println("no name");
}Output:
no name
instanceof pattern matching (Java 16+)
Object o = "hello";
if (o instanceof String s) {
System.out.println(s.toUpperCase());
}Output:
HELLO
Unsigned right shift
int x = -8;
System.out.println(x >> 1); // -4 (sign-preserving)
System.out.println(x >>> 28); // 15 (zero-fill)Output:
-4 15
⚠️ Common Mistakes
- Using == to compare Strings — it compares references; use .equals() for content.
- Confusing & with && — & always evaluates both sides (no short-circuit) and is also bitwise.
- Integer overflow silently wrapping — `Integer.MAX_VALUE + 1` becomes negative; use Math.addExact() to detect.
- Assuming >> zero-fills — it sign-extends for negative numbers; use >>> for zero-fill.
🎯 Interview Questions
Real questions asked at top product and service-based companies.
Q1.What is short-circuit evaluation?Beginner
&& stops evaluating if the left side is false; || stops if the left is true. The right operand isn't executed — useful for guarding expressions like `obj != null && obj.method()`.
Q2.What's the difference between & and &&?Intermediate
&& is logical AND with short-circuiting (operands are boolean). & is bitwise AND on integers, and as a boolean operator it evaluates BOTH sides with no short-circuit.
Q3.What does >>> do?Intermediate
Unsigned (zero-fill) right shift. Unlike >> which preserves the sign bit, >>> always shifts in zeros from the left. Java-specific; useful for treating ints as unsigned.
Q4.Why can't you compare Strings with ==?Beginner
== compares object references. Two Strings with the same content may be different objects (unless interned), so == can be false. Use .equals() to compare characters.
🧠 Quick Summary
- Arithmetic + concatenates Strings; / truncates for ints; % is remainder.
- && and || short-circuit; & and | do not.
- >> sign-extends, >>> zero-fills (Java-specific).
- instanceof supports pattern matching (Java 16+).
- Compare object content with .equals(), not ==.