Java operators mirror C-style languages: arithmetic, comparison, logical, and assignment. Operator precedence matters—use parentheses when clarity beats memorization.
Arithmetic and assignment
int a = 10, b = 3;
System.out.println(a / b); // 3 — integer division
System.out.println(a % b); // 1 — remainder
a += 2;
Comparison and logical
==,!=,<,>— compare primitives; for objects useequals&&,||,!— short-circuit boolean logicinstanceof— type check at runtime
Important interview questions and answers
- Q: Why does
10 / 4equal 2 in int math?
A: Integer division truncates toward zero; cast todoublefor fractional results. - Q: == on Strings?
A: Compares references unless interned; useequalsfor value equality.
Self-check
- What is the result of
17 % 5? - When should you use
equalsinstead of==?