Operators
Java's full operator set and the precedence and associativity rules that decide how an expression parses. Interviewers use operator questions for quick puzzles about integer division, short-circuiting and shifts.
part ofJavaoverview, primer and where to startread it →on this pageshowhide
explore
- Arithmetic Operators5 questions
- Relational & Equality Operators5 questions
- Logical Operators & Short-Circuiting5 questions
- Bitwise & Shift Operators5 questions
- Assignment & Compound Assignment5 questions
- Ternary Conditional Operator5 questions
- instanceof Operator5 questions
- Operator Precedence & Associativity5 questions
questions
page 2 of 2In Java, what does an expression like 1 << 33 evaluate to, and why? Explain shift-distance modulo behavior.
basics
~20 sThe shift amount is reduced using a remainder. For int it's the amount mod 32, for long it's mod 64. So 1 << 33 is the same as 1 << 1 = 2, not 0.
What surprising results arise from integer promotion and sign extension when applying bitwise and shift operators to byte, short, and char, and how do you handle them safely?
basics
~20 sbyte, short, and char are widened to int before any bitwise or shift operation. A negative byte gets its high bits filled with 1s (sign extension), so masking with & 0xFF is often needed to get the value you expect.
When does an instanceof expression fail to compile rather than simply evaluate to false?
basics
~20 sIf the compiler can prove the test could never possibly be true — because the two types have no possible relationship — instanceof is a compile error, not a false result. It also won't compile with a primitive on the left.
Why are long instanceof / type-check chains often considered a design smell, and what should you reach for instead?
basics
~20 sChains of instanceof checks scatter type-specific logic and must be edited every time a new subtype is added. Prefer polymorphism (an overridden method on each type) so each type owns its behavior. For closed sets, sealed types + pattern-matching switch give safe, exhaustive handling.
How does short-circuit evaluation influence how you order operands in a compound condition, and what pitfalls arise from relying on it?
basics
~20 sBecause && and || stop early, the order of checks matters: put guards (like null checks) and cheap, likely-decisive tests first so the expensive or unsafe ones run only when needed. The pitfall is hiding required side effects inside an operand that may get skipped.
How does operator precedence differ from operand evaluation order in Java? Use an expression with side effects to illustrate.
basics
~20 sPrecedence decides how operators group their operands; evaluation order decides which operand runs first in time. In Java operands always run left to right, no matter the precedence, so a higher-precedence operator can still have its operands computed later.
Why can == sometimes return true and sometimes false when comparing two Integer objects with the same value?
basics
~20 sInteger is an object, so == compares references, not the number. Java caches small boxed values (-128 to 127), so two boxed ints in that range can share one object and be == ; outside that range they are different objects and == is false. Always use .equals() or unbox to int.
When ordering objects, why can't you use relational operators, and what do you use instead?
basics
~10 sRelational operators (<, >) only work on numbers, not objects. To order objects you implement Comparable's compareTo (or pass a Comparator) and check whether its result is negative, zero, or positive.
When both branches of a ternary are reference (object) types rather than numerics, how does Java decide the result type?
basics
~20 sFor object branches, Java finds a common parent type both branches share — roughly their nearest shared supertype. If one branch is null, the result takes the other branch's type. The result is the most specific type that can hold either value.
As a tech lead, how would you set a team policy around operator precedence to prevent subtle bugs, and where do you draw the line between relying on the table and using explicit parentheses?
basics
~20 sLet obvious arithmetic precedence stand (a * b + c), but require parentheses whenever you mix different operator families — especially bitwise with comparison, or assignment/ternary in chains. Back it with linter rules so it's automatic, not memory.
showing 31–40 of 40