Java Operators β€” Arithmetic, Logical, Bitwise, Comparison

Operators are Java's built-in one- or two-character functions β€” +, ==, &&, <<. Java has six categories: arithmetic, comparison, logical, bitwise, assignment, and the special ones (instanceof, ternary ?:, cast, new).

Arithmetic

int sum  = 3 + 4;      // 7
int diff = 10 - 3;     // 7
int mul  = 6 * 7;      // 42
int div  = 10 / 3;     // 3   β€” integer division truncates
int mod  = 10 % 3;     // 1   β€” remainder
int inc  = i++;        // post-increment β€” returns OLD, then increments
int pre  = ++i;        // pre-increment  β€” increments, then returns NEW

Comparison (relational and equality)

a == b        // reference equality for objects, value for primitives
a != b
a < b     a <= b
a > b     a >= b
a instanceof Shape       // type check β€” true if a is a Shape or subclass
a instanceof Shape s     // pattern matching (Java 16+) β€” binds `s` if true

Logical β€” short-circuit

a && b        // true if BOTH true; doesn't evaluate b if a is false
a || b        // true if EITHER true; doesn't evaluate b if a is true
!a            // negation

The short-circuit behaviour is load-bearing: if (s != null && s.length() > 0) is safe only because s.length() is never called when s is null.

Bitwise (and non-short-circuit logical)

a & b         // bitwise AND β€” on booleans, also AND but evaluates both sides
a | b         // bitwise OR
a ^ b         // bitwise XOR
~a            // bitwise NOT
a << n        // shift left by n (multiply by 2ⁿ)
a >> n        // arithmetic shift right (sign-preserving)
a >>> n       // logical shift right (fills with zero)

Assignment and compound assignment

int x = 10;
x += 5;       // x = x + 5
x -= 3;       // x = x - 3
x *= 2;       // x = x * 2
x /= 4;       // x = x / 4
x %= 3;       // x = x % 3
x <<= 1;      // shift, AND, OR, XOR compound versions also exist

The ternary operator

String status = active ? "ON" : "OFF";
int abs = n >= 0 ? n : -n;

Useful for short conditions. Nesting more than one level becomes hard to read β€” use an if/else or a switch expression instead.

Operator precedence (top = highest)

PrecedenceOperators
1(), [], . (member access), ::, postfix ++ --
2unary + - ! ~, prefix ++ --, cast, new
3* / %
4+ -
5<< >> >>>
6< <= > >= instanceof
7== !=
8-11&, ^, |, &&, ||
12?: (right-associative)
13= += -= ... (right-associative)

When in doubt, add parentheses. (a & b) == 0 is a classic bug without parens β€” & has lower precedence than ==.

The string concatenation trap

"a" + 1 + 2       // "a12" β€” left-to-right, string wins once it appears
1 + 2 + "a"       // "3a"  β€” addition until the string joins
"result: " + (a + b)   // always use parens for clarity

All sub-topics

Common mistakes

  • Using == on Strings β€” compares references. Use .equals().
  • Integer overflow β€” Integer.MAX_VALUE + 1 wraps to Integer.MIN_VALUE. Use Math.addExact() to throw on overflow.
  • Float equality β€” 0.1 + 0.2 == 0.3 is false. Compare with a tolerance: Math.abs(a - b) < 1e-9.
  • Precedence surprises β€” the bitwise-vs-equality bug above. Parenthesise.

Try it & related tools

Test any operator expression in the Java Online Compiler. Bitwise tricks often appear in the Base64 tool's encoding logic.